Calculate the X^n using recursive function.

#include <stdio.h>

float fnpower(int, int);

int main(void)
{
int base, index;
float res;

printf("\nEnter the base and index :");
scanf("%d%d",&base, &index);

res = fnpower(base, index);

printf("\n%d raised to %d is %f", base, index, res);

return 0;
}

float fnpower(int base, int index)
{
if(index == 0)
return 1.0;
else if(index > 0)
return base * fnpower(base, index - 1);
else
return (1.0 / base) * fnpower(base, index + 1);
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin