Greetings all.
I am running into an issue with a program I am working with. In the power function everything seems to be working but it returns some funky value.
Any help would be great.
Code:
#include <stdio.h>
#include <math.h>
void run(void);
int getnumb2(void);
int getnumb1(void);
int power( int semisol, int number, int to );
int main(){
run();
return (0);
}
void run(void){
int x, y, answer;
printf("\nThis program will take a number and an exponent. For example, the program will preform x to the power of y.");
x=getnumb1();//gets the first the number; the base
y=getnumb2();//gets the exponent
answer=power(x, x, y);//a recursive function that solves the equation//
printf("\n%d to the power of %d is %d\n\n\n",x,y,answer);
}
int getnumb1(void){//gets the first the number; the base
int x;
printf("\nPlease enter a value for x: ");
scanf("%d",&x);
return x;
}
int getnumb2(void){//gets the exponent
int y;
printf("\nPlease enter a value for y: ");
scanf("%d",&y);
return y;
}
int power(int semisol, int number, int to){//a recursive function that solves the equation/ x to the power of y or 'number' to the power of 'to'
if (to==0){
semisol=1;
printf("test of zero");//tst lones
return semisol;
}
else if (to==1){
printf("%d %d",to,semisol);//test line only
printf(" test of one ");//test line only
return semisol;
}
else if (to>1){
printf("%d %d",to,semisol);//test line only
printf(" Stop ");//test line only
semisol= semisol * number;
to = to - 1;
power(semisol, number, to);
}
}