In this example we’ll learn How to find factorial in C++.
The factorial of a positive integer n is equal to 1*2*3*…n. You will learn to calculate the factorial of a number using for loop in this example.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { unsigned int n; unsigned long long factorial = 1; cout << "Enter a positive integer: "; cin >> n; for(int i = 1; i <=n; ++i) { factorial *= i; } cout << "Factorial of " << n << " = " << factorial; return 0; } |
Output: