Factorial Program Help

I have looked through all the forums and have not found out what I needed(despite all the topics about Factorials)

I am to write a program that will take in a value inputted by the user(only integers one through ten) and out its factorial.
I have been working on this code for some time, and just can't figure out whats wrong. I know it has something to do with my for loop because the variable counter isnt incrementing like I need it to. I'm just not sure how to solve this. Any help would be wonderful.

I don't want the answer though, as this is for an assignment for school. I just need some guidance as to what I should be looking for and how I could go about solving it. I want to understand what I'm doing and why it's working opposed to what I put. Thanks fellas :)

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
#include<iostream>
using namespace std;

int main()
{
    int number, factorial;
    
    cout<<"Enter an integer between one and ten: ";
    cin >>number;
     
     //data verification
    if(number < 0 && number>=10)
    {
            cout<<"Enter a vaild integer!!";
    }
    
    else
    {
        //for loop for factorial
        for( int counter = 1; counter > 11; counter++)
        {
            factorial = number * (number - counter);
            cout << number << "!= " << factorial <<endl;
        }
    }
    
    
    
    system("pause");
    return 0;
}
@knicole89

You have your for loop wrong. for( int counter = 1; counter > 11; counter++) should be for( int counter = 1; counter < 11; counter++)
Well that helps a lot lol I feel like an idiot.
But now its displaying a series of things
Such as:

Ill enter 3 as the integer and this is what will be outputted:

Enter an integer between one and ten: 3
3!= 6
3!= 3
3!= 0
3!= -3
3!= -6
3!= -9
3!= -12
3!= -15
3!= -18
Press any key to continue . . .
@knicole89

To get the factorial, use this for loop instead of the one you wrote. The loop must count backwards, so the factorial of 5! = 5x4x3x2x1, which is 120, factorial of 3! = 3x2x1, is 6.
1
2
3
4
       factorial = 1;  
        for( int counter = number; counter > 1; counter--)
            factorial = (factorial * counter);
    cout << number << "!= " << factorial <<endl;


EDIT: Loop doesn't HAVE TO count backwards. This works also..
1
2
3
4
       factorial = 1;
       for( int x=1; x <=number; x++)
            factorial = (factorial * x);
   cout << number << "!= " << factorial <<endl;
Last edited on
Okay, I got it now!
But, just a question, our instructor said the basic formula for a factorial was n(n-1). Which I would assume factorial = factorial(factorial - 1) right?

So, why would you set counter equal to the number and then only multiply by the counter instead of subtracting it?
I know these question may sound juvenile, but it never hurts to understand something(:
No problem. Learning is good. Each time through the loop, the number is being subtracted by one. So, factorial is equal 1, to start off. Counter is equal to the number inputted. Let's say 5. So now factorial is 1x5 = 5. Counter is reduced by 1 with counter-- in the for loop. Factorial is now 5x4 equal 20. Then it's 20x3 = 60, 60x2 = 120 and the loop is finished since we don't need the multiplying by 1, hence the >1 in the loop.

You may want to add more coding so you check for correct input. As it right now, you can type 20, and the program runs anyway. This if(number < 0 && number>=10) should be if(number < 1 || number > 10) It'll print that the number isn't right, but proceeds anyway. You need a do/while check
Last edited on
Ahh okay, Thanks for explaining that for me!! (:
Topic archived. No new replies allowed.