What's the error?

–2 votes
asked Oct 1 by (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 by (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 by (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.
...