My assignment for my CS class is to create a program in C to solve for a power without utilizing the pow() command and while utilizing the While loop. My program works, however, I am having a problem with my "undefined" answers and I don't know how to fix it.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
double base, exponent, result, result2, result3;
printf("enter two numbers to fit this format x^n\n");
printf("x can be any number. insert n only as a whole number.\n");
scanf("%lf %lf", & base, &exponent);
if ((base == 0) && (exponent == 0)) {
printf("UNDEFINED\n");
} else if ((base == 0) && (exponent <= -1)) {
printf("UNDEFINED\n");
}
result = 1;
result2 = 1;
while (exponent != 0) {
if (exponent <= -1) {
result2 = result2 * base;
exponent = exponent + 1;
} else if (exponent > -1) {
result = result * base;
exponent = exponent - 1;
}
}
result3 = 1 / result2;
if ((result3 < 1) && (result > 0)) {
printf("The answer is %lf\n", result3);
} else {
printf("the answer is %lf\n", result);
}
}
When I enter my base and exponent that should give me an "UNDEFINED" answer (power and exponent are both 0 OR when the base is 0 and the exponent is a negative number), my answer comes up as "undefined" but the program also tells me the solution is "1". How do I get the "answer is 1" thing to go away?