Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
fixup: CLI arguments for C, CPP, Go, Ruby
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Zak <[email protected]>
  • Loading branch information
rjzak committed Aug 1, 2022
1 parent 363c20c commit a61b897
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Zig/*/zig-out/
.idea/
.vscode/
result
*.wasm
a.out
21 changes: 15 additions & 6 deletions C++/fibonacci/fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// Simple Program to calculate Fibonacci Sequence of an integer input
#include <iostream>
#include <cstdlib>
using namespace std;

int FibonacciSequence(int num) {
if(num <= 1) {
return num ;
}
return FibonacciSequence(num-1) + FibonacciSequence(num-2);
}
int main(){
cout << "Enter the Number" << endl;
int n ;
cin >> n ;

cout << "Fibonacci Sequence term at " << n << " " << "is " << FibonacciSequence(n) << endl;
int main(int argc, char* argv[]){
int n;
if (argc > 1) {
for(int i = 1; i < argc; i++) {
n = atoi(argv[i]);
cout << "Fibonacci sequence number at index " << n << " is " << FibonacciSequence(n) << endl;
}
} else {
cout << "Which Fibonacci index to find? ";
cout.flush();
cin >> n;
cout << "Fibonacci sequence number at index " << n << " is " << FibonacciSequence(n) << endl;
}
}
22 changes: 17 additions & 5 deletions C/fibonacci/fibonacci.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#include <stdio.h>
#include <stdlib.h>

int FibonacciSequence(int num) {
if(num <= 1) {
return num ;
}
return FibonacciSequence(num-1) + FibonacciSequence(num-2);
}
int main(){
printf("Enter the Number\n");
int n ;
scanf("%d",&n);

printf("Fibonacci Sequence term at %d is %d " , n , FibonacciSequence(n));
int main(int argc, char* argv[]){
int n;
if (argc > 1) {
int i;
for(i = 1; i < argc; i++) {
n = atoi(argv[i]);
printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n));
}
} else {
printf("Which Fibonacci index to find? ");
int matched = scanf("%d", &n);
if (matched < 1) {
n = 10;
}
printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n));
}
}
3 changes: 3 additions & 0 deletions Go/fibonacci/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/enarx/codex/Go/fibonacci

go 1.17
33 changes: 23 additions & 10 deletions Go/fibonacci/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
// Simple Program to calculate fibonacci of input

package main

import "fmt"
func FibonacciRecursion(n int) int {
import (
"fmt"
"strconv"
"os"
)

func FibonacciSequence(n int) int {
if n <= 1 {
return n
}
return FibonacciRecursion(n-1) + FibonacciRecursion(n-2)
return FibonacciSequence(n-1) + FibonacciSequence(n-2)
}

func main(){
fmt.Print("Enter number : ")
var n int
fmt.Scanln(&n)

fmt.Println("Fibonacci of", n , "is", FibonacciRecursion(n));
if len(os.Args) > 1 {
for _, arg := range os.Args[1:] {
n, _ := strconv.Atoi(arg)
fmt.Printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n));
}
} else {
var input string
fmt.Print("Which Fibonacci index to find? ")
fmt.Scanln(&input)
n, err := strconv.Atoi(input)
if err != nil {
n = 10
}
fmt.Printf("Fibonacci sequence number at index %d is %d\n" , n , FibonacciSequence(n));
}
}
16 changes: 13 additions & 3 deletions Ruby/fibonacci/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
def fibonacci( n )
def FibonacciSequence( n )
return n if ( 0..1 ).include? n
( fibonacci( n - 1 ) + fibonacci( n - 2 ) )
( FibonacciSequence( n - 1 ) + FibonacciSequence( n - 2 ) )
end

if ARGV.length > 0
ARGV.each { |arg|
n = arg.to_i
puts "Fibonacci sequence number at index #{n} is #{FibonacciSequence(n)}"
}
else
puts "Which Fibonacci index to find? "
n = ARGF.gets.to_i
puts "Fibonacci sequence number at index #{n} is #{FibonacciSequence(n)}"
end
puts fibonacci( 5 )

0 comments on commit a61b897

Please sign in to comment.