fibonacci_calculation
This commit is contained in:
parent
489e34df26
commit
bb9a14c6e8
2 changed files with 46 additions and 0 deletions
8
8-fibonacci/Cargo.toml
Normal file
8
8-fibonacci/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "fibonacci"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
38
8-fibonacci/src/main.rs
Normal file
38
8-fibonacci/src/main.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use std::io;
|
||||||
|
fn get_input()->u32
|
||||||
|
{
|
||||||
|
loop
|
||||||
|
{
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.expect("Failed to read line");
|
||||||
|
match input.trim().parse()
|
||||||
|
{
|
||||||
|
Ok(num) => return num,
|
||||||
|
Err(_) =>
|
||||||
|
{
|
||||||
|
println!("Expected Valid Number");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn fibo(val:u32)->u128
|
||||||
|
{
|
||||||
|
let mut a = 0;
|
||||||
|
let mut b = 1;
|
||||||
|
let mut c;
|
||||||
|
for _element in 2..=val
|
||||||
|
{
|
||||||
|
c = a+b;
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
println!("Hello, world!");
|
||||||
|
println!("Give a Number for Fibonacci Calculation ↓");
|
||||||
|
println!("Result = {}",fibo(get_input()));
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue