Thread: help me, factorial in C recursive

  1. #1
    Registered User
    Join Date
    Sep 2006

    help me, factorial in C recursive

    can you guys please tell me why this wont do the factorial math!!

    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    int n, total, i;
    total = 1;
    printf("Enter a natural number: ");
    scanf ("%d", &n);
    while (n < 0)
    printf ("Error Please Re-Enter Your Natural Number");
    scanf ("%d", &n);
    i = 1;
    for (n > 0; i = n; i++)
    total = total * i;
    printf ("%d", total);
    return 0;
    Last edited by Dave_Sinkula; 09-23-2006 at 07:55 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    Registered User
    Join Date
    Jun 2006
    The mistake is in the following line:
    for (n > 0; i = n; i++)
    Try to think about it: you should make i count from 1 onwards, and stop when it reaches n.

  3. #3
    Registered User
    Join Date
    Mar 2006
    fflush(stdin) is undefined.
    getch() is nonportable.
    main() returns int.
    The factorial is not recursively calculated.
    Do you actually know what recursion means?
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    Do you actually know what recursion means?
    See my signature.

    In addition to what others have stated (especially the lack of recursion, and the broken for()):
    Indent your code. This will make it so much easier to read - for you and the board.

    For for():
    The first is a statement that occurs at the start of the loop.
    The second part is the condition that must hold true before each iteration.
    The third part is a statement to run after each iteration. So:
    for(x = 0; x < 10; ++x)
    Runs for ten iterations. (x starts at zero, the loop runs while x is less than 10, and after each iteration, x is incremented) This is equivalent to:
    x = 0;
    while(x < 10)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP me Factorial Recursive
    By helpme1234 in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2006, 10:51 PM
  2. Recursive factorial, what is wrong with my code?
    By xephyr in forum C Programming
    Replies: 10
    Last Post: 07-25-2004, 09:09 AM
  3. recursive factorial function
    By brianptodd in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2003, 12:56 AM
  4. recursive factorial prblm
    By rippascal in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2002, 10:36 AM
Website Security Test