The following header is deprected, instead try to use <iostream>
#include <iostream.h>
You shouldn't have the equals sign here.
#define q = 3
I have no idea what this is intended to do.
int n;
if (n < 0) return 0;
Again, your code it a little unclear here. It says to declare a variable f, set it equal to 1 and then multiply by n. This f and the int f previously declared are different.
for (n=1; n <= q; n++)
{
double f = 1;
{f *= n;}
}
When the loop finishes, the double f should go out of scope. So the value that is being printed at the end is the uninitialized value of the int f.
(If it were to compile.)
It couldn't hurt to use more descriptive names, too. Maybe something like this.
#include <iostream> // let's try to use a compiler from this millenium
int main()
{
int n, result = 1, value = 3;
for ( n = 1; n <= value; n++ )
{
result *= n;
}
std::cout << value << "! = " << result << std::endl;
std::cin.get(); // pause ???
return 0;
}
/* my output
3! = 6
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Skill Endorsements: 38
You cant use int for such big numbers as 100!, since int usually can handle 32bit value.
solution is google: "c++ big integer"
pecet
Junior Poster in Training
95 posts since Oct 2009
Reputation Points: 11
Solved Threads: 17
Skill Endorsements: 0
You cant use int for such big numbers as 100!, since int usually can handle 32bit value.
solution is google: "c++ big integer"
1.) I suggest you take a closer look at your datatype limits. A 32-bit signed integer can hold values ranging from about -2.1 billion to +2.1 billion. A long long can go out to about +/- 9 Quintillion (yes Quintillion is a real number, it's 18-zeros)
2.) Why are we posting in a 5-year-old thread??? Especially when the thread necro is just someone trying to scam a homework assignment...
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5
well to keep kicking the dead dog. Fboady it is true that a 32bit int is aprox -2 bil to 2 bil but 100! is 9.3326215443944152681699238856267e+157 which is way out of range for an int on even a 128 bit system.
NathanOliver
Posting Virtuoso
1,516 posts since Apr 2009
Reputation Points: 281
Solved Threads: 278
Skill Endorsements: 3
well to keep kicking the dead dog. Fboady it is true that a 32bit int is aprox -2 bil to 2 bil but 100! is 9.3326215443944152681699238856267e+157 which is way out of range for an int on even a 128 bit system.
You are correct.
I realized what pecet was actually saying way after I posted. I completely overlooked the factorial symbol as merely an exclamation point.
I was wrong :$
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5
Please ignore fr3aky's poorly formatted unreadable code. It will teach you very little that is useful.
fr3aky, here we do not not give programs to people, we help them solve their own problems. And when we post code the help, the code is formatted, readable, and useful.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
1.)Have you read the thread? The answer to your problem has already been discussed.
2.)This is at least the second time this thread has been necroed. Please start your own.
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5