You are using a version without Ads of this website. Please, consider donating:

### I have a question here

write a program which inputs an integer value ,checks its positive and outputs its factorial ,using the formulas n!=1x2x3....x n?

I didnt understand tha factorial formulas???
and i just did this code

include <iostream>
using namespace std;

int main()
{
int n;
cout<<"enter number\n";
cin>>n;
if(n>0)
cout<<n=1*2*3*4*5*6*7*8*9*10<<endl;
return 0;
}

Last edited on
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
is this true
i feel it doesnt match the formula in the question!!!!

#include <iostream>
using namespace std;

int main ()
{
int n;
int res=1;
cout<<"enter a number\n";
cin>>n;
if(n>0)
{
for(int i=1;i<=n;i++)
res=res*i;
}
cout<<res<<endl;

system("pause");

return 0;
}
Last edited on
http://en.wikipedia.org/wiki/Factorial
Basic math. Sixth grade, I believe.
Last edited on
@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.
Last edited on
Hi there!
Quote from Wikipedia:
 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, 5! = 1 x 2 x 3 x 4 x 5 = 150

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:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include using namespace 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; long long 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! ;)

 ``12345678910111213141516171819202122232425262728`` ``````#include 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 -> "<
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: