Program : FIND FACTORIAL OF A GIVEN NUMBER
Program to find factorial of a number using For Loop
Check out Program
#include<iostream.h>
#include<conio.h>
// Find factorial of a Given Number
int main()
{
long double factorial = 1; /* factorial has long double type which take 8 bytes in memory and it can store number within range of 1.7E -308 to 1.7E + 308 */
int n;
cout <<"Enter a Number : "<<endl; // endl for new line
cin >> n; // Take Integer value from user and store in variable n
for(int i =n; i>0; i--)
factorial *= i;
/* You Know Factorial is product of all positive number integers less than or equal to n. In above loop we initialize i equal to value of n that is taken from user, then set condition that i > 0 because we do multiplication here if i = 0 then it will give result 0. Then in loop statement is factorial * = i , it means that factorial = factorial * i , Now we already assign factorial value as 1, if we don't assign value 1 it will give error or junk value, because we are doing multiply if there is no number to multiply with it give junk value. In this we use decrement of value of i from n to 1, so it will multiply number like this n * (n-1) * (n-2) * ... * 2 * 1 . In this loop i didn't put braces because any loop or condition if you didn't put braces then it will automatic consider next line or till the termination(;) it will consider that part inside loop. So next line to loop inside the loop after that loop scope is finished.
*/
cout << n << "! = " << factorial <<endl;
getch(); //http://cplspls.blogspot.com
return 0;
}
OUTPUT:
Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
long double factorial = 1;
int n;
cout <<"Enter a Number : "<<endl;
cin >> n;
for(int i =n; i>0; i--)
factorial *= i;
cout << n << "! = " << factorial <<endl;
getch();
return 0;
}
Program to find factorial of a number using For Loop
Check out Program
#include<iostream.h>
#include<conio.h>
// Find factorial of a Given Number
int main()
{
long double factorial = 1; /* factorial has long double type which take 8 bytes in memory and it can store number within range of 1.7E -308 to 1.7E + 308 */
int n;
cout <<"Enter a Number : "<<endl; // endl for new line
cin >> n; // Take Integer value from user and store in variable n
for(int i =n; i>0; i--)
factorial *= i;
/* You Know Factorial is product of all positive number integers less than or equal to n. In above loop we initialize i equal to value of n that is taken from user, then set condition that i > 0 because we do multiplication here if i = 0 then it will give result 0. Then in loop statement is factorial * = i , it means that factorial = factorial * i , Now we already assign factorial value as 1, if we don't assign value 1 it will give error or junk value, because we are doing multiply if there is no number to multiply with it give junk value. In this we use decrement of value of i from n to 1, so it will multiply number like this n * (n-1) * (n-2) * ... * 2 * 1 . In this loop i didn't put braces because any loop or condition if you didn't put braces then it will automatic consider next line or till the termination(;) it will consider that part inside loop. So next line to loop inside the loop after that loop scope is finished.
*/
cout << n << "! = " << factorial <<endl;
getch(); //http://cplspls.blogspot.com
return 0;
}
OUTPUT:
Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
long double factorial = 1;
int n;
cout <<"Enter a Number : "<<endl;
cin >> n;
for(int i =n; i>0; i--)
factorial *= i;
cout << n << "! = " << factorial <<endl;
getch();
return 0;
}
great example...
ReplyDeleteIndustrial Training Companies in India
Factorial Program in C++
ReplyDeletein c++ you can easily write Factorial of any number, it is the product of an integer and all the integers below it for example factorial of 5 is
5! = 5 * 4 * 3 * 2 * 1 = 120. factorial program in c++ is very simple and easy.