to ger factorial of n (n!) you should multiplicata all numbers from 1 to n. So
1! = 1
2! = 1 × 2 = 2
3! = 1 × 2 × 3 = 6
4! = 1 × 2 × 3 × 4 = 24
n! = 1 × 2 × ... × n
Yes this is true. You didn't have to ask the forum about this, you could have just googled it.
@MiiNiPaa nahh, that's rude. Maybe seventh.
Also wrap your code in a code tag to make it easier to read. And proper syntax is to do any calculations before outputting. And what would you do if it was negative? You should end up with two while loops.
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
Other examples:
Suppose we wan to find the factorial of 3, then 3! = 1 x 2 x 3 = 6
0! = 1 ; by convention 1! = 1
2! = 1x2 = 2 3! = 1x2x3 = 6 4! = 1x2x3x4 = 24 5! = 1x2x3x4x5 = 120 6! = 1x2x3x4x5x6 = 720
....
....
.... n! = 1x2x3x4x5x6x7x8x........xn ; all the way up to the nth number
Here is some code, I hope you like it:
#include <iostream>
usingnamespace std; //Not advisable to use "using namespace std;" outside of main due to possible conflicts,
// or using it at all. Instead, use "using std::foo"
int main()
{
/*=============================================================================================*/
cout << "Enter integer (enter non numeric input to quit): ";
/*=============================================================================================*/
int numb;
longlong result = 1; // long long to handle large numbers
/*OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO*/
while(cin >> numb) // If you enter anything besides numbers, this returns false and the "while" loop is skipped
// because it is expecting an integer since you declared "numb" to be of type int.
// Else it is executed.
{
if (numb < 0) // If numb smaller then zero, execute, else skip
{
cout << "Sorry, you must enter positive values." << endl;
cout << "Enter integer number (enter non numeric input to quit): ";
continue; //"continue" at the beginning of while loop, skip rest of body.
}
/*===============================================================================*/
if (numb == 0) //If numb equals zero, execute, else skip
{
cout << "0! = 1"; // Because 0! = 1 by convention
break; // if numb equals 0, "break" here out of "while" loop, skip rest of body
}
/*================================================================================*/
for (int i = 1; i <= numb; i++)
{
result = result * i; // This is were calculation takes place
}
/*===============================================================================*/
cout << numb << "! = " << result << endl << endl; // Output
/*===============================================================================*/
result = 1; // reseting "result"
/*===============================================================================*/
cout << "Enter integer (enter non numeric input to quit): ";
}
/*OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO*/
cout << "Out of while loop." << endl << endl;
return 0; // You don't necessarily have to put "return 0"
// at the end of main. It is added automatically if it doesn't find it
}
There are numbers of way to do it...but I think it suffice for now.
Keep up the good work, don't quit because you are doing fine, trust me! ;)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
double factorial=1;
double number;
cout<<"Enter a number(non negative) to calculate factorial: ";
cin>>number;
while(number>=1){
factorial*=number;
number--;
}//end while
if(number<0)
cout<<"Please non negative numbers -> "<<number<<endl;
else
cout<<"Factorial is: "<<factorial<<endl;
//type your favorite pause statement HERE e.j "cin.ignore();" or whatever you want
return 0;
}//end main