Thread: These 2 recursive funtions won't work with me.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    These 2 recursive funtions won't work with me.

    these are 2 programs that I need to depend on recursive functions when writing them but they give me wrong answers.Plz help

    Code:
    This program is for calculating the factorial of a number the user inputs:
    
    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int n;
        int factorial(int n);
        int y;
        y=factorial(n);
        printf("enter the number you wish to calculate its factorial:");
        scanf("%d" ,&n);
        printf("the answer is:%d" ,y);
    }
    int factorial(int n){
        if(n==0)
        return (1);
        else
        return (n*factorial(n-1));
    }
    Code:
    This program is for calculating numbers raised to integer powers.
    
    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int x,y;
        int power(int x, int y);
        int d;
        d=power(x,y);
        printf("enter the number of the base then the number of the power:");
        scanf("%d%d" ,&x, &y);
        printf("the answer is:%d" ,d);
    }
    int power(x,y){
        if(y==0)
        return (1);
        else
        return (x*power(x,(y-1)));
    }

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    In your First Code just place

    Code:
    y=factorial(n);
    after
    Code:
    scanf("%d" ,&n);
    line
    You are doing the same mistake in the second program

    just place
    Code:
    d=power(x,y);
    after scanf statement

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    thanks it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM
  3. Searching a binary search tree - doesn't work
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-11-2005, 03:53 PM
  4. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 05:51 PM
  5. Recursive Function
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 03:41 PM