This program takes a positive integer from user and calculates the factorial of that number. Suppose, user enters 6 then,
#include <bits/stdc++.h>
using namespace std;
int factorial(int n) {
if(n > 1)
return n * factorial(n-1);
else
return 1;
}
int main() {
int n;
cin >> n;
int result = factorial(n);
cout << result << endl;
return 0;
}
Factorial will be equal to 1*2*3*4*5*6 = 720
You'll learn to find the factorial of a number using a recursive function in this example.
If you face any problem with the code please put your question into the comment box below....
#include <bits/stdc++.h>
using namespace std;
int factorial(int n) {
if(n > 1)
return n * factorial(n-1);
else
return 1;
}
int main() {
int n;
cin >> n;
int result = factorial(n);
cout << result << endl;
return 0;
}
No comments:
Post a Comment