
-----------------------------------
krishon
Tue Jun 17, 2003 3:09 pm

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

//---------------------------------------------------------------------------

#include 
#include 
#include 
#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

-----------------------------------
krishon
Tue Jun 17, 2003 3:50 pm


-----------------------------------
can someone help or does neone know?!?!?!?

-----------------------------------
Catalyst
Tue Jun 17, 2003 3:54 pm


-----------------------------------
for (i = num;i = 0; i--) 

turing has messed u up
in c++:

= is assignment (u are setting i to 0)
== is comparison

-----------------------------------
krishon
Tue Jun 17, 2003 3:56 pm


-----------------------------------
ohhhhh...ok, thx, i forgot bout that

-----------------------------------
Catalyst
Tue Jun 17, 2003 3:57 pm


-----------------------------------
these are the kind of mistakes that will slow ur debug drastically

-----------------------------------
krishon
Tue Jun 17, 2003 4:02 pm


-----------------------------------
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
Tue Jun 17, 2003 4:59 pm


-----------------------------------
First of all, what's up with all of those includes?

Here's how I'd do it..

#include  //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> j; cout > j;}
