Created
April 10, 2018 10:13
-
-
Save newton-migosi/1574ecddd1ba219225903ef57281fd94 to your computer and use it in GitHub Desktop.
Rust CLI app for getting nth fibonacci number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
fn main() { | |
//nth fibonacci number generator | |
loop{ | |
println!("Enter position of fibonacci you want."); | |
let mut pos = String::from(""); | |
io::stdin().read_line(&mut pos) | |
.expect("Could not read input."); | |
let pos_int: isize = match pos.trim().parse() { | |
Ok(val) => val, | |
Err(_) => continue | |
}; | |
let fibo = get_fibo(pos_int); | |
println!("{}", fibo); | |
} | |
} | |
fn get_fibo(pos: isize) -> isize { | |
let mut counter = 0; | |
let mut a = 0; | |
let mut b = 1; | |
let mut fibo = 1; | |
while counter < pos - 1 { | |
fibo = a + b; | |
a = b; | |
b = fibo; | |
counter = counter + 1; | |
} | |
fibo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment