-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35.cpp
32 lines (26 loc) · 806 Bytes
/
35.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Write a C program to find power of any number using recursion
*/
#include <stdio.h>
// Function to calculate power using recursion
double power(double base, int exponent) {
if (exponent == 0) {
return 1; // Base case: any number raised to 0 is 1
} else if (exponent > 0) {
return base * power(base, exponent - 1); // Recursive case for positive exponent
} else {
return 1 / power(base, -exponent); // Handle negative exponent
}
}
// Main function
int main() {
double base;
int exponent;
printf("Enter the base: ");
scanf("%lf", &base);
printf("Enter the exponent: ");
scanf("%d", &exponent);
double result = power(base, exponent);
printf("%.2lf raised to the power of %d is %.2lf\n", base, exponent, result);
return 0;
}