# Thread: Issues with segmentation fault

1. ## Issues with segmentation fault

Hi

I am currently running into a problem that is confusing.
The program takes two values, a base and an exponent. It calculates the number. It seems to work fine except when I input a negative value for the exponent; the program will stop and state segmentation fault. Any help would be awesome. Thanks in advance.

Code:
```#include <stdio.h>
#include <math.h>

void run(void);
float getnumb2(void);
float getnumb1(void);
float power( float semisol, float number, float exponent );

int main(){

run();

return (0);
}

void run(void){
printf("\nThis program will take a number and an exponent. For example, the program will preform x to the power of y.\n");
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%f to the power of %f is %f\n\n\n",x,y,answer);
}

float getnumb1(void){//gets the first the number; the base
float x;
printf("\nPlease enter a value for x(the base): ");
scanf("%f",&x);
return x;
}

float getnumb2(void){//gets the exponent
float y;
printf("\nPlease enter a value for y(the exponent): ");
scanf("%f",&y);
return y;
}

float power(float semisol, float number, float exponent){//a recursive function that solves the equation/ x to the power of y or 'number' to the power of 'to'

if (exponent  == 0){
semisol=1;
return semisol;
}
else if (exponent == 1){
return semisol;
}
else if (exponent > 1){
semisol= semisol * number;
exponent = exponent - 1;
semisol=power(semisol, number,  exponent);
return semisol;
}
else if(exponent < -1){
semisol= 1 / (semisol * number);
semisol = power(semisol, number, exponent);
return semisol;
}
else if(exponent == -1){
return semisol;
}

}```

2. Never mind, I think I figured it out.

3. Code:
```else if(exponent < -1){
semisol= 1 / (semisol * number);
semisol = power(semisol, number, exponent);
return semisol;
}```
Never ends.

4. First of all, fix your indentation because as is the posted code is very hard to read.
For negative exponents, add one to the exponent before calling power() recursively.
Code:
```else if (exponent < -1) {
semisol= 1 / (semisol * number);
semisol = power(semisol, number, exponent + 1);
return semisol;
}```

5. Thanks, it works better. Now, I just have to figure out the math part of it.