...

/

Solution Review 1: Find The Factorial

Solution Review 1: Find The Factorial

This lesson gives a detailed solution review to the challenge in the previous lesson.

We'll cover the following...

Solution

Press + to interact
fn test(n:i32) {
let mut factorial = 1; // define a mutable variable factorial
if n < 0 { // check if factorial is less than zero
println!("0"); // print 0
}
else if n == 0 { // check if factorial is equal to 0
println!("1"); // print 1
}
else // go here if the above two conditions are false
{
for i in 1..n + 1{
factorial = factorial * i
}
println!("{}", factorial); // print the factorial
}
}
fn main(){
print!("factorial (4) : ");
test(4);
print!("factorial (6) : ");
test(6);
}

Explanation

  • On ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy