Thread: HELP me Factorial Recursive

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    HELP me Factorial Recursive

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

    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>

    main()
    {
    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);

    fflush(stdin);
    getch();
    return 0;
    }

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    This should probably be in 'C Programming' not 'C++ Programming'.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Use code tags, please

    Code:
    for (n > 0; i = n; i++)
    This for loop is wrong. What you probably want is for (int i = 1; i <= n; i++)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ... and beyond the above posts, it should also be mentioned that your method isn't recursive as the topic would imply. Now, when you wrap the code in code tags... I'll tell you about the several things that aren't standard in your code.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Please stop cross-posting.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /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;}

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Please stop cross-posting.
    The suggestion was made to put it in the C forum. My guess is that the OP just followed the suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive factorial, what is wrong with my code?
    By xephyr in forum C Programming
    Replies: 10
    Last Post: 07-25-2004, 09:09 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  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, 11:36 AM