In this program, we will learn how to find factorial of a given number using C++ program? Here, we will implement this program with and without using user define function.
Logic:
- Input a number
- Initialize the factorial variable with 1
- Initialize the loop counter with N (Run loop from number (N) to 1)
- Multiply the loop counter's value with factorial variable
Program to find factorial using loop in C++
Enter an integer number: 6
Factorial of 6 is = 720
What is factorial?
Answer:
Factorial !
Example: 4! is shorthand for 4 × 3 × 2 × 1
The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1. Examples:
|
Calculating From the Previous Value
We can easily calculate a factorial from the previous one:
As a table:
n | n! | ||
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 × 1 | = 2 × 1! | = 2 |
3 | 3 × 2 × 1 | = 3 × 2! | = 6 |
4 | 4 × 3 × 2 × 1 | = 4 × 3! | = 24 |
5 | 5 × 4 × 3 × 2 × 1 | = 5 × 4! | = 120 |
6 | etc | etc |
- To work out 6!, multiply 120 by 6 to get 720
- To work out 7!, multiply 720 by 7 to get 5040
- And so on
So the rule is:
n! = n × (n−1)!
Which says
"the factorial of any number is that number times the factorial of (that number minus 1)"
So 10! = 10 × 9!, ... and 125! = 125 × 124!, etc.
What About "0!"
Zero Factorial is interesting ... it is generally agreed that 0! = 1.
It may seem funny that multiplying no numbers together results in 1, but let's follow the pattern backwards from, say, 4! like this:
And in many equations using 0! = 1 just makes sense.
//Faizah Akter
ReplyDelete//202010061
#include
using namespace std;
class B
{
public:
int check()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for (int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
};
int main()
{
B obj;
obj.check();
}
Mahbubur rahman Limon
ReplyDeleteId:202030070
#include
using namespace std;
class Factor
{
public:
int count()
{
int counter, n, fact = 1;
cout<<"Enter an integer Number :";
cin>>n;
for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}
cout<<"Factorial of value is = "<<fact;
return 0;
}
};
int main()
{
Factor obj;
obj.count();
}
//Faizah Akter
ReplyDelete//202010061
#include
using namespace std;
class B
{
public:
int check()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for (int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
};
int main()
{
B obj;
obj.check();
}
//Sumaiya Mariya
ReplyDelete//ID:201520785
#include
using namespace std;
class B
{
public:
int c()
{
int n,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>n;
for (int i=1;i<=n;i++)
{
factorial=factorial*i;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
};
int main()
{
B obj;
obj.c();
}
//Robin Ahmed
ReplyDelete//201910109
#include
using namespace std;
int main (){
int number,factorial = 1;
cout<< "enter the number "<>number;
if(number < 0)
cout << "can't find the factorial for negative number";
else if (number<=1)
cout << number << "!="<=2; counter --)
{factorial=factorial*counter;}
cout<< number << "!= "<<factorial;
}
return 0;
}
//Monika Akter Ratna
ReplyDelete//202010275
#include
using namespace std;
int main (){
int num,fac = 1;
cout<< "enter the number "<>num;
if(num < 0)
cout << "can't find the factorial ";
else if (num<=1)
cout << num << "! = "<=2; con --)
{fac=fac*con;}
cout<< num << "! = "<<fac;
}
return 0;
}