So, I had to make an exponent calculator without using pow() or any other predefined functions. It's supposed to use recursion.
Below is my current code. The issue is, my program is supposed to handle negative bases, but my attempt to reprompt the user to enter a positive base with a while loop has been failing.
Code:
#include <stdio.h>
float exponential(float base, int exponent);
int main(){
float base, result;
int exponent;
printf("Enter base: ");
scanf("%f", &base);
while(base < 0.0);
{
printf("Sorry, the base is not positive.");
printf("Enter base: ");
scanf("%f", &base);
}
printf("Enter the exponent: ");
scanf("%d", &exponent);
result = exponential(base, exponent);
printf("Exponential calculation result: %f^%d = %f", base, exponent, result);
return 0;
}
float exponential(float base, int exponent){
if(exponent == 0){
return 1;
}
else if (exponent < 0){
return 1.0 / (base * exponential(base, abs(exponent) - 1));
}
else{
return base * exponential(base, exponent - 1);
}
}
Currently, the while loop executes even if the initial base entered is positive, and I'm not sure why this is the case.
Thanks in advance for any assistance!