Processing math: 100%

Pages

Thursday, 10 December 2015

Factorial of a number using C++



Factorial of a numebr n:

Factorial of a number n is the product of the numbers from 1 upto n. It is denoted by n!.

Example:  Factorial of  3 is 3! = 1 \times 2 \times 3 = 6
                 Factorial of 5 is 5! = 1 \times 2 \times 3 \times 4 \times 5 = 120



Algorithm : 

  1. Start the program
  2. Read the number n
  3. Initialize i = 1 and factorial =1
  4. Multiply factorial with i and store at factorial
  5. Increment the i
  6. Perform the step 4 and 5 until i = n
  7. Stop the program



#include"iostream"

using namespace std;

int main()

{

    int num, factorial=1;

    cout<<" Enter a number to find the factorial :  ";

    cin>>num;

    for(int i=1;i<=num; i++)

    {

        factorial=factorial*i;

    }

cout<<"Factorial of the number is ="<<factorial << endl;
return 0;

}


INPUT :      Enter the number to find the factorial : 8
OUTPUT :  Factorial of the number is = 40320


No comments:

Post a Comment