• We will be taking the forum down for maintenance at [5:15 am CDT] - in 15 minutes. We should be down less than 1 hour.
  • 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

tessaesque

Bring us a shrubbery!
DP Veteran
Joined
Apr 13, 2011
Messages
15,910
Reaction score
12,630
Location
Plano, Texas
Gender
Female
Political Leaning
Other
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:

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

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

Would you post your entire code?
 
Would you post your entire code?

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

#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 = number * counter;
//Display results
cout << "The factorial of " << number << " 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
 
Suggestion would be to list the entire "for loop" code segment. It appears that you are modifying your loop control variable within the loop itself. Variable scope is kind of tricky in C++.

You will need two nested "loops"; the outer loop to repeat for "control" numbers 2..60 and the inner loop to calculate/print the factorials for each given control number. Try making (and testing) the "factorial" function first then invoking it from within the controlling for 2..60 loop.

The FOR loop for C++

1). Factorial Using Loop Example Program In C++ - C++ Programming Concepts @ little drops @ thiyagaraaj.com
 
Last edited:
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?

Good morning, Tessaesque. :2wave:

I am constantly amazed at the mental power that some people possess! I speak several foreign languages, but your post is so far beyond my ability to comprehend what you are asking, it could be someone speaking a language that would be a familiar to someone visiting from one of Neptune's moons! Extreme Kudos to you from someone who had to say "huh?," and Good Luck in your quest! WOW! :thumbs:
 
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):

Your loop

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


should look more like

//Loop to calculate factorial

while (counter <= number)
{
factorial = factorial* counter;
counter++;
}

//end loop

//Display results
cout << "The factorial of " << number << " is " << factorial << "." << endl;
 
Your loop

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


should look more like

//Loop to calculate factorial

while (counter <= number)
{
factorial = factorial* counter;
counter++;
}

//end loop

//Display results
cout << "The factorial of " << number << " is " << factorial << "." << endl;

Yep. Put a semicolon at the end of your while statement, which meant that the while statement looped performing nothing.

Simple mistake.

Do you use indentation when you code? If not, its a good habit to get into now, rather than later. We should get the mods to add
Code:
 tags.
 
Yep. Put a semicolon at the end of your while statement, which meant that the while statement looped performing nothing.

Simple mistake.

Do you use indentation when you code? If not, its a good habit to get into now, rather than later. We should get the mods to add
Code:
 tags.[/QUOTE]

That's why I removed it in my response to tess.  The opening brace was also in the wrong location.  And yes, when I write code, I indent, but when responding to someone else, I mimic their style...
 
That's why I removed it in my response to tess. The opening brace was also in the wrong location. And yes, when I write code, I indent, but when responding to someone else, I mimic their style...

Sorry, my response was to tess; incorrectly quoted your msg
 
I don't know anything about C++, but I believe the formula should be

factorial = counter * factorial

For int factorial = 1 and int counter = 1, it will work out like this (I think, but again I don't know anything about C++ so if i'm just confusing you ignore me)

counter = 1
factorial = 1 * 1 = 1

then our variables become

counter = 2
factorial = 2 * 1 = 2

then our variables become

counter = 3
factorial = 3 * 2 = 6

then our variables become

counter = 4
factorial = 4 * 6 = 24

then our variables become

counter = 5
factorial = 5 * 24 = 120


run this loop while 'counter' is less than or equal to 'number' :)
 
Your loop

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


should look more like

//Loop to calculate factorial

while (counter <= number)
{
factorial = factorial* counter;
counter++;
}

//end loop

//Display results
cout << "The factorial of " << number << " is " << factorial << "." << endl;

Good morning, AP! :2wave:

I should have guessed that you would be able to understand Tessaesque's question, and respond! Wow, I have some brainy friends! Was Ancient Sanskrit easy, too? :) Kudos to you both! :thumbs:
 
Good morning, Tessaesque. :2wave:

I am constantly amazed at the mental power that some people possess! I speak several foreign languages, but your post is so far beyond my ability to comprehend what you are asking, it could be someone speaking a language that would be a familiar to someone visiting from one of Neptune's moons! Extreme Kudos to you from someone who had to say "huh?," and Good Luck in your quest! WOW! :thumbs:

I had been programming (writing computer code) for decades and in many languages (Ada, Basic, Turbo Pascal, COBOL, FORTRAN and various assembly level languages). I speak some Spanish and some more English. Computers (compliers and interpreters) are very unforgiving and will always do exactly what they are told to do - rather than what they "should" do. ;)
 
Good morning, AP! :2wave:

I should have guessed that you would be able to understand Tessaesque's question, and respond! Wow, I have some brainy friends! Was Ancient Sanskrit easy, too? :) Kudos to you both! :thumbs:

Good morning pg. It's rare when I can post anything during the day, but I'm inbetween auditors' questions at the moment...
 
When I change the coding to what Paul provided it no longer returns any results when I run the program. The cursor just moves to a blank line and blinks at me.
 
Once you format your loop properly, it's going to run forever, because you're not incrementing your counter. When you do increment your counter, note that the criteria of "counter > 0" is unnecessary because the counter will always be greater than 0 if you're increasing it every iteration.

Also System("Pause") does nothing in the context of your program.
 
When I change the coding to what Paul provided it no longer returns any results when I run the program. The cursor just moves to a blank line and blinks at me.

Please post your new code.
 
Holy crap, I figured it out.

Praise the flying spaghetti monster in all his gluten-y glory!

Just 6 more weeks and I can put all of this behind me....

Edit: Thank you, everybody, for all of your help!
 
Once you format your loop properly, it's going to run forever, because you're not incrementing your counter. When you do increment your counter, note that the criteria of "counter > 0" is unnecessary because the counter will always be greater than 0 if you're increasing it every iteration.

Also System("Pause") does nothing in the context of your program.

Our professor requires the system("pause"); line for "good practices".
 
Holy crap, I figured it out.

Praise the flying spaghetti monster in all his gluten-y glory!

Just 6 more weeks and I can put all of this behind me....
Here here!! Better thee than me!

My head hurts!!:2razz:
 
Our professor requires the system("pause"); line for "good practices".

Oh dear God. Smack him/her for me, and everyone else. He/She should be teaching you to prompt for user input(i.e. Press any key to continue... ), if he/she wants a controlled stop at the end of the program prior to completion.
 
Once you format your loop properly, it's going to run forever, because you're not incrementing your counter. When you do increment your counter, note that the criteria of "counter > 0" is unnecessary because the counter will always be greater than 0 if you're increasing it every iteration.

Also System("Pause") does nothing in the context of your program.

The for loop construct handles (generates code to do) that automagically in C++, note the bolded portion below.

/* Example Program For Factorial Value Using For Loop In C++
little drops @ thiyagaraaj.com


Coded By:THIYAGARAAJ MP */


#include<iostream>
#include<conio.h>


using namespace std;


int main()
{


// Variable Declaration
int counter, n, fact = 1;


// Get Input Value
cout<<"Enter the Number :";
cin>>n;


//for Loop Block
for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}


cout<<n<<" Factorial Value Is "<<fact;
// Wait For Output Screen
getch();
return 0;
}
 
I had been programming (writing computer code) for decades and in many languages (Ada, Basic, Turbo Pascal, COBOL, FORTRAN and various assembly level languages). I speak some Spanish and some more English. Computers (compliers and interpreters) are very unforgiving and will always do exactly what they are told to do - rather than what they "should" do. ;)

Good morning, ttwttt. :2wave:

In honest awe of you and your prowess in that field! Now if we could apply mathematical formulas to have our representatives in DC do exactly what they are supposed to do rather than what they want to do??....we really need you to work on that! Really, really..... :thumbs:
 
The for loop construct handles (generates code to do) that automagically in C++, note the bolded portion below.

/* Example Program For Factorial Value Using For Loop In C++
little drops @ thiyagaraaj.com


Coded By:THIYAGARAAJ MP */


#include<iostream>
#include<conio.h>


using namespace std;


int main()
{


// Variable Declaration
int counter, n, fact = 1;


// Get Input Value
cout<<"Enter the Number :";
cin>>n;


//for Loop Block
for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}


cout<<n<<" Factorial Value Is "<<fact;
// Wait For Output Screen
getch();
return 0;
}

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's only for the "for" loop, I don't think that's the case for the "while" loop.

But I may be wrong.

It works to increment an integer anywhere in the code as necessary...
 
Back
Top Bottom