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

Here is the link: http://en.wikipedia.org/wiki/Factorial

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
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! ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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
Topic archived. No new replies allowed.