100k views
3 votes
3- Write a C++ program by using for loop that will ask for an integer as a parameter and display its factorial. Hint factorial (5)=5^ * 4^ * 3^ * 2^ * 1

1 Answer

6 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter number: ";

cin >> n;

int fact = 1;

for(int i=2; i<=n; i++) {

fact *= i;

}

cout << n << "! = " << fact << endl;

}

Step-by-step explanation:

Another cool way to do this is to write a recursive function. But here specifically a for loop was asked.

User AtomRiot
by
5.7k points