c++ program to find factorial of any positive number;
/*This is a c++ program to find factorial of any number given by user.
we use while loop in it.
store 1 in fact then store fact*i=1*1 in it
when i will come after inrement 2 will store in it
when i will 3 and come ,2 is stored stored in fact now 2*3= 6 will store in it
It will going on till i becomes equal to num*/
#include <iostream> //library
using namespace std;
int main() //tell about main function the execution will start from here
{
int i=1,num; //we declare num and inizialize the count i.e i
double fact=1; //we also start fact from 1
cout<<"Enter number = "; //display message on console
cin>>num; //input number
if (num>0) //applying if so that number should be positive
{
while (i<=num) //applying condion that number should equal to or greater than i
{
fact=fact*i; //now store the product value of fact*i in fact
i++;
}
cout<<"The Factorial = "<<fact<<endl; //we should use {} braces to enclose the body of if,else if,else ;
} //loop and condition in parenthesis ()
else if (num==0) //we can use if separatly but no else if,else
{
cout<<"The factorial = 1"<<endl;
}
else if (num<0)
{
cout<<"Please enter positive number.\n";
}
return 0;
}
/*This is a c++ program to find factorial of any number given by user.
we use while loop in it.
store 1 in fact then store fact*i=1*1 in it
when i will come after inrement 2 will store in it
when i will 3 and come ,2 is stored stored in fact now 2*3= 6 will store in it
It will going on till i becomes equal to num*/
#include <iostream> //library
using namespace std;
int main() //tell about main function the execution will start from here
{
int i=1,num; //we declare num and inizialize the count i.e i
double fact=1; //we also start fact from 1
cout<<"Enter number = "; //display message on console
cin>>num; //input number
if (num>0) //applying if so that number should be positive
{
while (i<=num) //applying condion that number should equal to or greater than i
{
fact=fact*i; //now store the product value of fact*i in fact
i++;
}
cout<<"The Factorial = "<<fact<<endl; //we should use {} braces to enclose the body of if,else if,else ;
} //loop and condition in parenthesis ()
else if (num==0) //we can use if separatly but no else if,else
{
cout<<"The factorial = 1"<<endl;
}
else if (num<0)
{
cout<<"Please enter positive number.\n";
}
return 0;
}
No comments:
Post a Comment