complete fibonacci

This commit is contained in:
2026-10-02 14:08:51 +01:00
parent 8ac1739ed0
commit 2aff2d45c1
4 changed files with 43 additions and 1 deletions
Generated
+4
View File
@@ -12,6 +12,10 @@ checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
name = "common-programming-concepts"
version = "0.1.0"
[[package]]
name = "common-programming-concepts-fibonacci"
version = "0.1.0"
[[package]]
name = "common-programming-concepts-temp-converter"
version = "0.1.0"
+1 -1
View File
@@ -1,5 +1,5 @@
[workspace]
members = ["common-programming-concepts", "common-programming-concepts-temp-converter","guessing-game"]
members = ["common-programming-concepts", "common-programming-concepts-fibonacci", "common-programming-concepts-temp-converter","guessing-game"]
resolver = "2"
[workspace.package]
@@ -0,0 +1,7 @@
[package]
name = "common-programming-concepts-fibonacci"
edition.workspace = true
version.workspace = true
license.workspace = true
[dependencies]
@@ -0,0 +1,31 @@
use std::io;
fn main() {
println!("welcome to the fibonacci calculator I guess");
println!("Please insert a number between 2 and 45");
let mut num = String::new();
io::stdin().read_line( &mut num).expect("failed to read line");
let num: u8 = match num.trim().parse() {
Ok(n) if n < 45 => n,
Ok(_) => panic!("oh no please insert correct number.... terminating"),
Err(_) => panic!("oh no please insert correct number.... terminating"),
};
println!("You have chosen {num}");
let fib = fibonacci(num);
println!("{fib}");
}
fn fibonacci(n: u8) -> u32 {
if n == 0 {
0
} else if n == 1 {
1
} else {
self::fibonacci(n - 1) + self::fibonacci(n - 2)
}
}