-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_FactorialProgram.cpp
More file actions
51 lines (41 loc) · 1016 Bytes
/
6_FactorialProgram.cpp
File metadata and controls
51 lines (41 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
int main() {
int n;
long factorial = 1.0;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
// Factorial of a number:
// 4! = 4*3*2*1 = 24
// 6! = 6*5*4*3*2*1 = 720
// Factorial of a number using Function..
// C++ program to find
// factorial of given number
// #include <iostream>
// using namespace std;
// // Function to find factorial
// // of given number
// unsigned int factorial(unsigned int n)
// {
// if (n == 0 || n == 1)
// return 1;
// return n * factorial(n - 1);
// }
// // Driver code
// int main()
// {
// int num = 5;
// cout << "Factorial of "
// << num << " is " << factorial(num) << endl;
// return 0;
// }