Thread: Recursion Problem

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Recursion Problem

    Hey Guys,

    Can any body help me out with the factorial problem below. I learnt scheme last semester and I am trying to use the same approach to find the factorial using recursion but it doesn't seem to work !



    Code:
    int main() {
    
                   int n;
                   printf("Enter a number for factorial :");
                   scanf("%d", &n);
                   factorial(n);
    
    }
    
    
    int factorial(int num){
    
    double ans =0;
    
            int print_factorial(int num) {
    	 if (num==0)
    	 return 1;
             else 
             ans = (num*print_factorial(num-1));
     
    } 
    
            printf("%d",ans);
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you aren't returning anything

    and you're trying to nest functions, which you can't do and makes no sense even if you could
    Last edited by ಠ_ಠ; 10-15-2009 at 10:01 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by kuma0177 View Post


    Code:
    int main() {
    
                   int n;
                   printf("Enter a number for factorial :");
                   scanf("%d", &n);
                   factorial(n);
    
    }
    
    
    int factorial(int num){
    
    double ans =0;
    
            int print_factorial(int num) {
    	 if (num==0)
    	 return 1;
             else 
             ans = (num*print_factorial(num-1));
     
    } 
    
            printf("%d",ans);
    }
    ok dude...hers the prob..

    try not nesting functions since its no use at all


    int factorial(int num){

    double ans =0;

    if (num==0)
    return 1;
    else
    ans = (num*factorial(num-1));

    }

    printf("%d",ans);
    }
    [/CODE]

    and the in the printf..."%d" is for decimals...try "%f" for doubles.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 09-21-2009, 09:50 PM
  2. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  3. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  4. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM