Author |
Message |
krishon
|
Posted: Tue Jun 17, 2003 3:09 pm Post subject: factorial |
|
|
hey, well, i'm back to programmin already, but this time in c++. i've been trying to figure out some problems that my school has so i'd be ready for next year. I'm tryin to figure out factorials ( for example the factorial of 5 = 5! = 5*4*3*2*1) I've finished it, but for some reason, the answer always comes out to 256 everytime i try it. heres the code
code: | //---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <conio.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
printf ("Enter a positive number.\n\n");
int num;
scanf ("%d", &num);
int factorial;
int i;
for (i = num;i = 0; i--)
factorial *=(num);
printf ("\nThe factorial of this number is %d" , factorial);
printf (".\n\n");
printf ("Hit any key to exit.\n");
getch ();
return 0;
}
//--------------------------------------------------------------------------- |
ne help would be great, thx |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
krishon
|
Posted: Tue Jun 17, 2003 3:50 pm Post subject: (No subject) |
|
|
can someone help or does neone know?!?!?!? |
|
|
|
|
 |
Catalyst

|
Posted: Tue Jun 17, 2003 3:54 pm Post subject: (No subject) |
|
|
code: | for (i = num;i = 0; i--) |
turing has messed u up
in c++:
= is assignment (u are setting i to 0)
== is comparison |
|
|
|
|
 |
krishon
|
Posted: Tue Jun 17, 2003 3:56 pm Post subject: (No subject) |
|
|
ohhhhh...ok, thx, i forgot bout that |
|
|
|
|
 |
Catalyst

|
Posted: Tue Jun 17, 2003 3:57 pm Post subject: (No subject) |
|
|
these are the kind of mistakes that will slow ur debug drastically |
|
|
|
|
 |
krishon
|
Posted: Tue Jun 17, 2003 4:02 pm Post subject: (No subject) |
|
|
wow...it still dusn't work i changed it to i == 0, and it still gives me 256 wutever number i enter...i'm lost |
|
|
|
|
 |
Martin

|
Posted: Tue Jun 17, 2003 4:59 pm Post subject: (No subject) |
|
|
First of all, what's up with all of those includes?
Here's how I'd do it..
code: | #include <iostream> //For cout/cin
int main()
{
int num; //The number to find the factorial of
cin>>num;
int i;
int fac=1;
for (i=1;i<=num;i++) fac*=i;
cout <<fac<<endl;
getch();
return 0;
} |
|
|
|
|
|
 |
krishon
|
Posted: Tue Jun 17, 2003 9:08 pm Post subject: (No subject) |
|
|
dun't u need the conio.h header file for getch...maybe cuz i'm using c++ builder 5, i dunno if that's different in 6 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
krishon
|
Posted: Tue Jun 17, 2003 9:12 pm Post subject: (No subject) |
|
|
hmm...ye..i fixed some problems, it all works now, thx...i saw my mistake, i multiplied it by the number instead of i  |
|
|
|
|
 |
SilverSprite
|
Posted: Sat Jun 21, 2003 7:50 pm Post subject: (No subject) |
|
|
I know this is a bit late.. but i just read over this post..
Here's how i would do it
code: | #include <iostream.h>
int factorial (int i) {if (i==1) return 1; else return i*factorial (i-1);}
int main() {int j; cin >> j; cout << factorial (j); cin >> j;} |
|
|
|
|
|
 |
|