Question #83684

Write a program that will display the result of n! using a loop. For example, if the user enters 5 for n, the program should display “factorial of is 120

Expert's answer

#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 << " is " << factorial;    
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS