1. Loops in C++

C++ Program to Find Factorial

For any positive number n, it’s factorial is given by:

factorial = 1*2*3...*n

Factorial of negative number cannot be found and factorial of 0 is 1.

Program

#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

Enter a positive integer: 12
Factorial of 12 = 479001600
Comments to: C++ Program to Find Factorial

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Login

Welcome to WikiCoders

Join the site and store the save posts for later, share your own articles
Join WikiCoder
Registration is closed.