Pages

Tuesday, 16 June 2015

Compute power of any number using recursion

Compute power of any number using recursion

Coding:

#include<stdio.h>
float power(float a,int x)
{
if(x==0)
return 1;
else
return a*power(a,x-1);
}
int main()
{
float b;
int y;
printf("Please enter the number:");
scanf("%f",&b);
printf("Please enter the power:");
scanf("%d",&y);
printf("Result is:%f",power(b,y));
return 0;
}

Output:
Please enter the number:2.25
Please enter the power:3
Result is:11.390625