Here are C++ programs to find factorial of a number using simple while-loop, functions, recursion, tail recursion, class and also a program to calculate factorial of first N numbers
Factorial of n is denoted by n! and it is calculated by the product of integer numbers from n to 1.
For example 3! = 3*2*1=6. Similarly n!=n*(n-1)........*2*1.
Factorial of n is denoted by n! and it is calculated by the product of integer numbers from n to 1.
For example 3! = 3*2*1=6. Similarly n!=n*(n-1)........*2*1.
C++ program to find factorial of number
#include<iostream>
using namespace std;
int main()
{
int number,fact=1,num2;
cout<<"Enter number ";
cin>>number;
num2=number;
while(number!=0)
{
fact*=number;
number--;
}
cout<<"Factorial of "<<num2<<" = "<<fact;
return 0;
}
using namespace std;
int main()
{
int number,fact=1,num2;
cout<<"Enter number ";
cin>>number;
num2=number;
while(number!=0)
{
fact*=number;
number--;
}
cout<<"Factorial of "<<num2<<" = "<<fact;
return 0;
}
C++ program to find factorial of number using function
#include<iostream>
using namespace std;
int fact(int num)
{
for(int i=num-1;i>1;i--)
num*=i;
return num;
}
int main()
{
int number,num2;
cout<<"Enter number ";
cin>>number;
num2=fact(number);
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
using namespace std;
int fact(int num)
{
for(int i=num-1;i>1;i--)
num*=i;
return num;
}
int main()
{
int number,num2;
cout<<"Enter number ";
cin>>number;
num2=fact(number);
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
C++ program to find factorial of number using recursion
#include<iostream>
using namespace std;
int fact(int num)
{
if(num==1)
return 1;
return num*fact(num-1);
}
int main()
{
int number,num2;
cout<<"Enter number ";
cin>>number;
num2=fact(number);
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
using namespace std;
int fact(int num)
{
if(num==1)
return 1;
return num*fact(num-1);
}
int main()
{
int number,num2;
cout<<"Enter number ";
cin>>number;
num2=fact(number);
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
C++ program to find factorial of number using tail recursion
#include<iostream>
using namespace std;
int fact(int num,int n)
{
if(n==1)
return num;
return fact(num*n,n-1);
}
int main()
{
int number,num2;
cout<<"Enter number ";
cin>>number;
num2=fact(number,number-1);
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
using namespace std;
int fact(int num,int n)
{
if(n==1)
return num;
return fact(num*n,n-1);
}
int main()
{
int number,num2;
cout<<"Enter number ";
cin>>number;
num2=fact(number,number-1);
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
C++ program to find factorial of number using class
#include<iostream>
using namespace std;
class calfact
{
private:
int number;
public:
void getnum(int i)
{
number=i;
}
int fact();
};
int calfact::fact()
{
for(int i=number-1;i>1;i--)
number*=i;
return number;
}
int main()
{
int number,num2;
calfact factorial;
cout<<"Enter number ";
cin>>number;
factorial.getnum(number);
num2=factorial.fact();
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
using namespace std;
class calfact
{
private:
int number;
public:
void getnum(int i)
{
number=i;
}
int fact();
};
int calfact::fact()
{
for(int i=number-1;i>1;i--)
number*=i;
return number;
}
int main()
{
int number,num2;
calfact factorial;
cout<<"Enter number ";
cin>>number;
factorial.getnum(number);
num2=factorial.fact();
cout<<"Factorial of "<<number<<" = "<<num2;
return 0;
}
No comments:
Post a Comment