Source Code
// 36. Program to calculate the power of a number using recursion
// Arpan Das - CST 2nd Year (VOCLET)
#include <stdio.h>
//#include <conio.h>
//#include <stdlib.h>
int power(int, int);
int main () {
int num, pwr, res=1;
//system("cls");
//clrscr();
printf("\t\t..:: calculate the power of a number using recursion ::..\n\n");
printf("please enter a number and power respectively: ");
scanf("%d%d", &num, &pwr);
res = power(num, pwr);
printf("\n%d^%d = %d", num, pwr, res);
//getch();
}
int power(int num, int pwr) {
if (pwr == 0) {
return 1;
} else {
return num * power(num, pwr-1);
}
}
/*int power(int num, int pwr, int res) {
if (pwr > 0) {
res *= num; //8
pwr--;//0
power(num, pwr, res); // 2, 0, 8
} else {
return res;
}
}*/
Output
..:: calculate the power of a number using recursion ::..
please enter a number and power respectively: 2 5
2^5 = 32