This is one of my first programs in C, so please bear with me! I wrote this code to calculate a base number raised to another given number. I got no compilation errors, except when I run my code, nothing happens. What am I doing wrong?
Thank you!
#include <stdio.h>
#include <stdlib.h>
int expCalculator(int base, int exponent) {
if (exponent == 0){
return 1;
}
else if (exponent % 2) {
return base * expCalculator(base, exponent - 1);
}
else {
int temp = expCalculator(base, exponent / 2);
return temp * temp;
}
}
int main() {
float base, answer;
int exponent;
int positiveBase;
char buffer[10];
positiveBase = 0;
while (positiveBase == 0){
printf("Enter a base number: ");
scanf(" %f", &base);
if (base > 0){
positiveBase = 1;
printf("Please enter an exponent value to raise the base to: ");
scanf(" %d", &exponent);
answer = expCalculator(base, abs(exponent));
gcvt(base, 10, buffer);
printf(buffer, " to the power of ", exponent, " is ", answer);
}
else {
printf("Please enter a positive base! Try again.");
}
}
return 0;
}