Skip to content

Instantly share code, notes, and snippets.

@qolop
Created May 19, 2016 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qolop/9dfaa230521a017d8c29467a40ad2ce1 to your computer and use it in GitHub Desktop.
Save qolop/9dfaa230521a017d8c29467a40ad2ce1 to your computer and use it in GitHub Desktop.
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