Oct 4, 2013

Program: Find Factorial of a Given Number

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;
}

Related Posts:

  • Using else if statement Using else if statement Else if statement is similar to if condition. In else-if condition you can give more than one condition like we have to give… Read More
  • Using Nested Loop  Using Nested Loop Nested Loop is using loop inside a loop like for loop is under for loop for example:  for(int i=0;i<10;i++) {for(int… Read More
  • Using Conditional Statements Using Conditional Statements The if statement is used to check the condition. If Condition is true Then particular code execute and if condition is … Read More
  • Using Nested Condition Using Nested Condition Nested Condition is if else condition that is used under if or else condition. Its condition within condition. Like : We have… Read More
  • Program : Show Sqaures of Number upto n terms Program : Show Sqaures of Number This program first take input from user that an integer value and show the squares of term upto the number that ent… Read More

2 comments:

  1. Factorial Program in C++

    in 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.

    ReplyDelete