Finding Factorial In C++

By
Advertisement
Definition of factorial ?
Factorial is non-integer number is the product of all number 1 to itself.
denoted by n! .

Example :  If u want to find the factorial of  number 6 then the factorial will be 
1*2*3*4*5*6= 720 .



Logic behind the factorial programming in C++ :


  1. First of all from definition we get how factorial can be calculated.
  2. Declare a variable named factorial and initialize it with 1 .
  3. Second we have to get the number from user.
  4. We have to multiply all numbers up to that given number.
    if number is 4 factorial= 1*2*3*4
  5. For this purpose For LOOP is very help full  which start from 1 and end on that number.

    Note : If we start it from 0 factorial will be zero.


#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
 int number;// Get the number from user
 int factorial=1; //declare and initial factorial with 1
 
 cout<<"Enter the Number to find factorial  ";
 cin>>number;
 
 for(int i=1;i<=number;i++)
 {
  factorial=factorial*i;
 }
 
 cout<<"\n Factorial of "<<number<<"! is " <<factorial<<endl;
 
 getch();
 
}

0 comments:

Post a Comment