Write a program on C++ to calculate and print the factorial of a number using a for loop. The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 is 4*3*2*1= 24.
#include <iostream> using namespace std; int main() { int n; cout << "Enter a positive integer: "; cin >> n; int factorial = 1; for (int i = n; i >= 1; --i) { factorial *= i; cout << i; if (i != 1) { cout << "*"; } } cout << "=" << factorial << endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.