C++ Program to Find Factorial of a Number using Iteration

In this C++ program, we will have a look at the factorial of a number in C++ using Iteration. Iteration is the process of the repetition of a method in order to generate a  sequence of outputs.

Factorial of a Number using Iteration in C++

#include<iostream>
using namespace std;
class fact
{
public:
int factorial(int n)
{
int i;
int pro = 1;
for(i = n; i > 1; i--)
{
pro = pro * i;
}
if(n == 0)
{
pro = 1;
}
return pro;
}
};

int main()
{
fact f;
int n;
cout<<"Please enter a number:\t";
cin>>n;
cout<<"\nFactorial of the number is:"<<n<<" is = "<<f.factorial(n);
return 0;
}

Output:

Please enter a number: 5

Factorial of the number  is: 120

Leave a Reply

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

Shopping cart