Factorial Program in C++

Google Advertisements

Prime Number in C++ Factorial of Number in C++

Factorial of a Number in C++

Factorial of any number is the product of an integer and all the integers below it for example factorial of 4 is
4! = 4 * 3 * 2 * 1 = 24

factorial program in c++

Factorial program in C++ using for Loop

Factorial Program in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int i, no, fact=1;
clrscr();
cout<<"Enter the any no. : ";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}

Output

Enter the any no. : 4
Factorial: 24

Factorial program using do-while loop

Example

#include<iostream.h>
#include<conio.h>

 void main()
  {
   long n, i, fact=1;
   clrscr();
   cout<<"Enter any num: ";
   cin>>n;
   i=n;
   if(n>=0)
    {
   do
    {
    fact=fact*i;
    i--;
    }
   while(i>0);
   cout<<"\nFactorial: "<<fact;
   }
   else
    {
   cout<<"fact of -ve num. is not possible";
    }
   getch();
  }

Output

Enter any num : 5
Factorial: 120

Factorial program using recursion in C++

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.

factorial of a number using recursion

Example

#include<iostream.h>
#include<conio.h>

void main()
{
int fact(int);
int f, n;
clrscr();
cout<<"Enter any num: ";
cin>>n;
f=fact(n);
cout<<"\nFactorial : "<<f;
getch();
}
int fact(int a)
{
if(a==1)
return(1);
else
{
return(a*fact(a-1));
}
}

Output

Enter any num: 6
Factorial: 720

Prime Number in C++ Factorial of Number in C++

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on hitesh.xc@gmail.com or 9999595223

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs