Trying to make a factorial program

I've been at this for a while. I need to make a factorial program for an assignment, but all I keep getting as a result is some large negative number. For example, when I input the number 5 into the program I get the result -1899959296.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	int number = 0;

	cout << "Enter a number: ";
	cin >> number;
	cout << number << "! = ";

	for (int i = 1; i <= number; ++i)
	{	
		number *= i;
	}

	cout << number;
	cout << endl;
}
Overflow. And it looks like for most inputs variable i will always be smaller than number?
Last edited on
I don't really know what you mean by overflow, but when I make i greater than number the result is just number
You are moving the goalpost with each iteration:

    while i ≤ number:
        number = number × i

See what goes wrong?

Hope this helps.
for (int i = 1; i <= number; ++i)

See that value, number? Every time you go round the loop you're making that value bigger, so i will never catch up, and you'll go round and round and round the loop so many times.
Check your for loop and the your number value that you are using.
If you are using the number variable as a limit in your for loop and you are still using it as the factorial value you will have the result you are presently having.

Edit:
In other words, you are changing the limit at every loop!
Last edited on
so, I've tried a couple things. Should I put a nested if statement in the loop? Or should I declare a different variable to put the value into? I have tried putting the value into another variable, but that is making it do nothing. (sorry I'm like brand-new to this haha)
Nope. You just need another variable.

Names matter, and even for a simple exercise like this one, the names you choose help clarify what you intend to do. "number" is not a helpful variable name.

If nothing else, state your equation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	// Program to calculate 
	//   y = n!
	// for a user-input n

	int n;      // the "n"
	int y = 1;  // the "y"
	int x;      // the loop counter := 1, 2, ..., n-1, n

	cout << "Enter a number: ";
	cin >> n;
	cout << n << "! = ";

Hope this helps.
Last edited on
No one here is interested in making fun of you.
We are all here because we had to learn this very same stuff ourselves, and remember how hard it was.

Factorial is defined as:

    n! = 1 * 2 * ... * (n-1) * n

A concrete example is:

    5! = 1 * 2 * 3 * 4 * 5

In order to calculate this with pen and paper, one might start combining terms:

  5! = 1 * 2 * 3 * 4 * 5
       ↓   ↓   |   |   |
       -----   |   |   |
         2     |   |   |
         ↓     ↓   |   |  
         -------   |   |
            6      |   |
            ↓      ↓   |
            --------   |
               24      |
                ↓      ↓
               ---------
                  120 

This is exactly what your program must do.

First, notice that the 5 (as in 5!) never changes. It is always a five. Your loop will always go 1→5.

Next, notice that the products are distinct things too...

n is your 5. It doesn't change.

x is your 1,2,3,... It changes on every iteration of your loop.

y is your running product. First it is 1*1. Then it is 1*2. Then it is 2*3:

  y=1  // (initial value)
  y*=1 // y==1
  y*=2 // y==2
  y*=3 // y==6
  y*=4 // y==24
  y*=5 // y==120

Your final y is the final product.

Hope this helps.
firstly your program is not looping a number of times based on the number that was entered by the user. As Duoas explained what a factoria is the expected amount of loops based on the number entered is 5. ie 5-1 then 5-2,..... @5-4 the loop should then break. so firstly adjust your program loop to reflect this.
Your program loops continuously because the value of number increases therefore "i" never reaches "number".
 5! = 1 * 2 * 3 * 4 * 5
       ↓   ↓   |   |   |
       -----   |   |   |
         2     |   |   |
         ↓     ↓   |   |  
         -------   |   |
            6      |   |
            ↓      ↓   |
            --------   |
               24      |
                ↓      ↓
               ---------
                  120 

if you put cout in your loop you would see that it does not behave like this.
Output of your program's varaiable "number":

5! = 5                                                                                                                                                      10                                                                                                                                                             
30                                                                                                                                                             
120                                                                                                                                                            
600                                                                                                                                                            
3600                                                                                                                                                           
25200                                                                                                                                                          
201600                                                                                                                                                         
1814400                                                                                                                                                        
18144000                                                                                                                                                       
199584000                                                                                                                                                      
-1899959296  


Because your code is doing:

 
5! = 5 * 2 * 3 * 4 * 5
     ↓   ↓   |   |   |
       ----- |   |   |
         10  |   |   |
         ↓   ↓   |   |  
         ------- |   |
            30   |   |
            ↓    ↓   |
            -------- |
               120   |
                ↓    ↓
               ---------
                  600
etc



Last edited on
Code correction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	int number = 0;

	cout << "Enter a number: ";
	cin >> number;
	cout << number << "! = ";
	int y=number;

	for (int i = 1; i <y; ++i)
	{
		number =number*(y-i);
		
	
	}

cout<<number;

}
thank you guys so much. I've never had this much progress when learning anything!! you guys are all awesome!
Topic archived. No new replies allowed.