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