Last active
August 2, 2019 11:08
-
-
Save adityasuseno/cd284c7d1526bdbcaaf38551bb9b83ba to your computer and use it in GitHub Desktop.
Counting a Factorial Using Rust
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 factorial(i: u64) -> u64 { | |
let mut acc = 1; | |
for num in 2..=i { | |
acc *= num; | |
} | |
acc | |
} | |
fn main() { | |
loop { | |
println!("Input Number: "); | |
let mut n = String::new(); | |
io::stdin().read_line(&mut n) | |
.expect("Failed to read line"); | |
let n: u64 = match n.trim().parse() { | |
Ok(num) => num, | |
Err(_) => continue, | |
}; | |
let answer: u64 = factorial(n); | |
println!("Result is: {}", answer); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment