Created
May 19, 2016 23:25
-
-
Save qolop/9dfaa230521a017d8c29467a40ad2ce1 to your computer and use it in GitHub Desktop.
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() { | |
println!("Please input the factorial."); | |
let mut s = String::new(); | |
io::stdin() | |
.read_line(&mut s) | |
.expect("failed to read line"); | |
let n: u8 = s.trim().parse().expect("Enter in a positive integer only"); | |
println!("Factorial of {} = {}", n, factorial(n)); | |
} | |
// We return an i64 to include bigger numerical results. | |
fn factorial(n: u8) -> i64 { | |
let mut total = n as i64; | |
for i in (1..n).rev() { | |
// rev() isn't needed and has no effect in this case | |
total *= i as i64; | |
} | |
total | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment