C Language Problem Write a recursive function that calculate
C Language Problem
Write a recursive function that calculates the value of a number (“base”) raised to a power (“power”). Assume that power is a nonnegative integer. Document your program.
Please Explain if needed
Solution
C Code:
#include <stdio.h>
long PowerFunction(int base, int power)
{
if(power == 0)
return 1;
return base * PowerFunction(base, power-1);
}
int main()
{
int base, power;
printf(\"Enter the base: \");
scanf(\"%i\", &base);
printf(\"Enter the power: \");
scanf(\"%i\", &power);
printf(\"%i raised to a power %i is %li\ \", base, power, PowerFunction(base, power));
}
Sample Output:
Enter the base : 2
Enter the power : 10
2 raised to a power 10 is 1024
