Power Function in C using Recursion
The Power Function
Source Code given below :
#include <stdio.h>
// prototype for power function
int power(int b, int n);
int main()
{
int base, pow;
printf("Enter a number and its power : \n");
scanf("%d %d",&base, &pow);
int res = power(base, pow);
printf("%d raised to power %d is %d\n",base, pow, res);
return 0;
}
// power function using recursion
int power(int b, int p)
{
if(p == 0) // base case
return 1;
else
return b*power(b,p-1); // recursive block
}
Comments
Post a Comment