#include <iostream>
// Function prototype for a recursive function that calculates the factorial of a given number
int factorial(int n);
int main() {
int num = 5;
// Call the factorial function
int result = factorial(num);
std::cout << "The factorial of " << num << " is " << result << std::endl;
return 0;
}
// Definition of the factorial function
int factorial(int n) {
if (n == 0) {
// Base case: the factorial of 0 is 1
return 1;
} else {
// Recursive case: the factorial of n is n * the factorial of n - 1
return n * factorial(n - 1);
}
}
output :
The factorial of 5 is 120
0 Comments