In this program will learn how to find the factorial of a number using for loop. The factorial number is positive integer n is equal to 1*2*3*…n.
Factorial of negative number can not be found and factorial of 0 is 1
Example: Find Factorial of a Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { int n; long factorial = 1; cout << "Enter a positive integer: "; cin >> n; for(int i = 1; i <= n; ++i) { factorial = factorial * i; } cout << "Factorial of " << n << " = " << factorial; return 0; } |
1 2 |
Enter a positive integer: 5 Factorial of 5 = 120 |