• This is a political forum that is non-biased/non-partisan and treats every person's position on topics equally. This debate forum is not aligned to any political party. In today's politics, many ideas are split between and even within all the political parties. Often we find ourselves agreeing on one platform but some topics break our mold. We are here to discuss them in a civil political debate. If this is your first visit to our political forums, be sure to check out the RULES. Registering for debate politics is necessary before posting. Register today to participate - it's free!

Formula Question - C++ Problem

That's only for the "for" loop, I don't think that's the case for the "while" loop.

But I may be wrong.

That is generally correct. Do while/until loops generally require separate "programmer" control of the test (loop control) variable value. What gets so tricky in C++ is variable scope, many use far too many global variables and pick lousy (duplicate) names for them at that. I prefer more programmer friendly languages like Turbo Pascal or, better yet, Ada. ;)
 
Last edited:
I think we're misunderstanding each other. I was noting that her original loop was an infinite loop, not your "for" loop.
 
I think we're misunderstanding each other. I was noting that her original loop was an infinite loop, not your "for" loop.

The original loop posted was decrementing, so yes, it was an infinite loop as it would have never have met the upper bound...
 
The original loop posted was decrementing, so yes, it was an infinite loop as it would have never have met the upper bound...

Code:
while (counter <= number && counter > 0);
factorial = number * counter;
//Display results
cout << "The factorial of " << number << " is " << factorial << "." << endl;
} //end loop

That's not decrementing.
 
Code:
while (counter <= number && counter > 0);
factorial = number * counter;
//Display results
cout << "The factorial of " << number << " is " << factorial << "." << endl;
} //end loop

That's not decrementing.

The OP was

(counter = 1; counter <= 60; counter--);
factorial = number * (number - counter);

Yes, it's decrementing...
 
I've been assigned a project in which I have to code a program that will calculate a factorial.

I've used several different versions of the formula that I've found through searches, but it still won't calculate anything correctly beyond factorials of 3. It appears as if my loop is the issue, but I can't figure out what I've done wrong.

The factorial must be calculated for any whole number between 2 and 60...here's what I put in the code:



not sure why it isn't working. Looks like every other code I'm finding is similar to that, except the parameters in the "for" clause are slightly different based on their limits.

Any ideas?

have you learned about subroutines yet?

I don't know C++, but factorials are the classic example of what is known as "recursion", where the code calculates the answer by calling itself while passing itself a subset of the problem

The factorial of almost every (positive) number is the number itself times the factorial of the number less 1:

void Factorial( int counter)
{
if(counter == 1)
return 1;
else
{
cout <<counter<<endl;
return Factorial(--counter) * counter;

}
}
 
If you haven't learned about subroutines then this:

#include <iostream>
using namespace std;

int main()
{
//Declare variables
int number = 1;
int factorial = 1;
int counter = 1;

//Prompt user for data
cout << "Enter a whole number between 2 and 60: ";
cin >> number;

//Enter the validation code
if (number > 1 && number < 61)
//Loop to calculate factorial
{
while (counter <= number && counter > 0);
factorial = factorial * counter;
counter++
//Display results
cout << "The factorial of " << counter << " is " << factorial << "." << endl;
} //end loop

else
//Display error
cout << "Entry invalid. Please enter an integer between 2 and 60." << endl;

system("pause");
return 0;
} //end of main
 
I've been playing w/it for awhile, so it isn't the same as what I originally posted, but here (please note it includes comments):
Where is "counter" incremented?
 
Hey Paul ... Ah ... looks like I'm late to the party.

tess figured it out this morning. Today was one of the few days I actually had some time to respond in the AM...
 
Factorial is a good time for you to learn recursive programming. You can call the following function in your main. Just place it in front of the main.


#include<assert.h>

int factorial(int n) {
assert(n > 0);
if(n == 1) {
return 1;
}
else {
return(n*factorial(n-1));
}
}
 
Reading this has made me more loopy than ever, which is difficult, considering the starting point.
 
Back
Top Bottom