The source code to calculate factorial using recursion is given below. The given program is compiled and executed successfully.
// Rust program to calculate the
// factorial using recursion
fn factorial(num:i32)->i32 {
if num == 1 {
return 1
} else {
return num * factorial(num-1)
}
}
fn main() {
let res = factorial(5);
println!("Factorial is: {}",res);
}
Output:
Factorial is: 120
Explanation:
In the above program, we created two functions factorial() and main(). The factorial() function is a recursive function, which is used to calculate the factorial of the given number.
In the main() function, we called the factorial() function and printed the result.
Program/Source Code:
The source code to calculate factorial using recursion is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two functions factorial() and main(). The factorial() function is a recursive function, which is used to calculate the factorial of the given number.
In the main() function, we called the factorial() function and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer