Write a function that calculates \(x^y\) using recursion.
$$x^y=\Bigg\{\begin{array}{c c c}1&if & y=0\\x \times x^{Y-1} & if & y>0\\ \frac{1}{x^{-y}} & if & y < 0\end{array}$$
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
double pow(double base, int expo)
{
if(expo == 0)
return 1;
if(expo > 0)
return base * pow(base, expo - 1);
return 1 / pow(base, -expo);
}
void main()
{
double base;
int expo;
printf("Enter thr base: ");
scanf("%lf", &base);
printf("Enter thr exponent: ");
scanf("%d", &expo);
printf("%.2lf ^ %d = %f", base, expo, pow(base, expo));
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Beverages