I wrote a function that receives a float number, changes it to its fractional part and returns its integer part. The fuction is structured as a if-else statement (to differentiate positive from negative numbers). However, only the first part of the statement is working. In the case below, only when I type negative or a zero number the code works; when I type a positive number, it only prints zero. The problem isn't with the algorithm for separating the integer from the fractional part as I also have structured a else-if with the first part being x positive, and also only the first part worked.

```
#include <stdio.h>
#include <stdlib.h>
//creates a function that changes the value of x to its
//fractional part and returns its integer part
float integerpart(float *x)
{
float integer = 0;
//If x is negative
if (*x<0)
{
//I use an algorithm to separate its integer from its fractional part
while (*x+1 <= integer)
{
integer--;
}
//I define the new value of x as its fractional part
*x = *x - (integer);
}
else if (*x=0)
{
*x = 0;
integer = 0;
}
//In the case in which x is positive
else
{
//I do the inverse algorithm
while (integer <= *x-1)
{
integer++;
}
*x-=integer;
}
//I return the integer part
return integer;
}
int main()
{
//Ask for the user to type a real number
float number;
printf("Type a real number:\n");
scanf("%f", &number);
//Uses the function to take the integer part of x
float integ;
integ = integerpart(&number);
//Prints the integer and fractional part
printf("Integer part = %f Fractional Part = %f\n", integ, number);
system("pause");
return 0;
}
```

`*x=0`

is assignment. You want`*x==0`

, which is comparison. – uneven_mark Oct 20 at 0:41`else if (*x=0) {}`

I see what you did there. BTW:`for()`

loops are your friend. Also: comparing a float value to zero is difficult. To say the least... – wildplasser Oct 20 at 0:41`floor`

or`ceiling`

? – Scott Hunter Oct 20 at 0:44`modff`

does what you want. – Blastfurnace Oct 20 at 1:06