Skip to content

Instantly share code, notes, and snippets.

@newton-migosi
Created April 10, 2018 10:13
Show Gist options
  • Save newton-migosi/1574ecddd1ba219225903ef57281fd94 to your computer and use it in GitHub Desktop.
Save newton-migosi/1574ecddd1ba219225903ef57281fd94 to your computer and use it in GitHub Desktop.
Rust CLI app for getting nth fibonacci number.
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