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

fixup: CLI arguments for C, CPP, Go, Ruby #25

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 16 additions & 7 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) {

unsigned int FibonacciSequence(unsigned 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[]){
unsigned 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;
}
}
24 changes: 18 additions & 6 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) {
unsigned int FibonacciSequence(unsigned 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[]){
unsigned 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("%u", &n);
if (matched < 1) {
rvolosatovs marked this conversation as resolved.
Show resolved Hide resolved
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
38 changes: 28 additions & 10 deletions Go/fibonacci/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
// Simple Program to calculate fibonacci of input

package main

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

func FibonacciSequence(n uint64) uint64 {
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, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
}
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.ParseUint(input, 10, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(-1)
}
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 )