Skip to content

Commit 198c783

Browse files
authored
Create c++3.cpp
1 parent 21b981b commit 198c783

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

c++3.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Factorial of n = 1*2*3*...*n
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int factorial(int);
7+
8+
int main() {
9+
int n, result;
10+
11+
cout << "Enter a non-negative number: ";
12+
cin >> n;
13+
14+
result = factorial(n);
15+
cout << "Factorial of " << n << " = " << result;
16+
return 0;
17+
}
18+
19+
int factorial(int n) {
20+
if (n > 1) {
21+
return n * factorial(n - 1);
22+
} else {
23+
return 1;
24+
}
25+
}

0 commit comments

Comments
 (0)