Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What's the error?

–2 votes
asked Oct 1, 2019 by Kelvin Mock (100 points)
#include <stdio.h>
#include <math.h>

int main()
{
    int n; //base
    printf("enter an integer: ");
    scanf("%d",&n);
    
    int k; //exponent
    printf("enter an exponent: ");
    scanf("%d",&k);
    
    int m = power(n,k);
    
    return 0;
}

int power(int base,int exponent)
{
    if (exponent<0)
    {
        exponent*=-1;
        return 0.1*power(base,exponent);
    }
    else if (exponent==0)
    {
        return 1;
    }
    else if (exponent==1)
    {
        return base;
    }
    else
    {
        return power(base,exponent)*power(base,exponent-1);
    }
}

2 Answers

0 votes
answered Oct 4, 2019 by karthikeyans (140 points)
#include <stdio.h>

#include <math.h>

int power(int base,int exponent);

int main()

{

    int n; //base

    printf("enter an integer: ");

    scanf("%d",&n);

    

    int k; //exponent

    printf("enter an exponent: ");

    scanf("%d",&k);

    

    int m =power(n,k);

    printf("%d",m);

    return 0;

}

int power(int base,int exponent)

{

    if (exponent<0)

    {

        exponent*=-1;

        return 0.1*power(base,exponent);

    }

    else if (exponent==0)

    {

        return 1;

    }

    else if (exponent==1)

    {

        return base;

    }

    else

    {

        return power(base,exponent)*power(base,exponent-1);

    }

}
0 votes
answered Oct 4, 2019 by Veeravajhula Saimathur (160 points)

In Recursive call try writing,

return base*power(base,exponent-1);

instead of this,

return power(base,exponent)*power(base,exponent-1);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...