/* This is a simple program to caluclate the facotrial

*
*/

#include <iostream>
using namespace std;

int main()
{
    int n;
    int fact = 1;

    cout<<"Enter a number & I'll give you its factorial: ";
    cin>>n;

    /* We need this condition to make sure the 0! is equal to 1

    *
    */

    if(n==0)
    cout<<"The factorial is 1\n";

    /* Inside the else statement consists a for loop to calculate the factorial

    *
    */

    else
    {
        for(int i = 1; i <= n; i++)
        {
            fact = fact * i;
        }

        cout<<"The factorial is "
                << fact << "\n";
    }
    /* System Pause used to pause the output window
    The latest version complier do not require it

    *
    */
    system("PAUSE");
    return 0;
}
 



 

< C++ >    < Home >