Factorial Program in C++

#include<iostream.h>
#include<conio.h>
void main()
{
   int i,fact=1,number;
   clrscr();
   cout<<"Enter the Number: ";    
   cin>>number;    
  for(i=1;i<=number;i++)
  {    
      fact=fact*i;    
  }    
  cout<<"Factorial of " <<number<<" is: "<<fact<<endl;  
  getch();
}  

Output:

Factorial Program in Cpp

Factorial Program in C++ Using while Loop:

#include<iostream.h>
#include<conio.h>
void main()
{
    int num, i, fact=1;
    cout<<"Enter the Number: ";
    cin>>num;
    i=num;
    while(i>=1)
    {
        fact = fact*i;
        i--;
    }
    cout<<"\nFactorial = "<<fact;
    cout<<endl;
    getch();
}

Output:

Factorial Program in Cpp

Factorial Program in C++ using Function:

#include<iostream.h>
#include<conio.h>
int Factorial(int);
void main()
{
    int num, fact;
    clrscr();
    cout<<"Enter the Number: ";
    cin>>num;
    fact = Factorial(num);
    cout<<"\nFactorial = "<<fact;
    cout<<endl;
    getch();
}
int Factorial(int n)
{
    int i, f=1;
    for(i=n; i>=1; i--)
        f = f*i;
    return f;
}

Output:

Factorial Program in Cpp

Leave a Reply

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