HELP me Factorial Recursive

This is a discussion on HELP me Factorial Recursive within the C++ Programming forums, part of the General Programming Boards category; can you guys tell me why this wont do the factorial math!! #include <stdio.h> #include <conio.h> #include <ctype.h> main() { ...

  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
    Portugal
    Posts
    7,532
    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++)
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,074
    ... 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,344
    >> 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, 10:09 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 10:34 PM
  3. recursive factorial function
    By brianptodd in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2003, 01:56 AM
  4. recursive factorial prblm
    By rippascal in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2002, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21