Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
factorial
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 #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

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

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 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;}
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 10 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: