# Thread: Finding the nearest even integer that's greater than the inputted number.

1. ## Finding the nearest even integer that's greater than the inputted number.

For the input of: 4.1, the output is supposed to be: 6. Please help, it seems that the output of the program that i wrote is just random.
Edit:
Code:
```
#include <stdio.h>

int main(void) {
while (1) {
double number0;
scanf("%lf",&number0);
int number,check1,check2,result1,result2;
number = (int)number0;
result1 = number++;
result2 = number+2;
check1 = result1%2;
check2 = result2%2;
if (check1==0)
printf("The nearest even integer that's greater than the entered number = %i. \n",result1);
else
printf ("The nearest even integer that's greater than the entered number = %i. \n", result2);
}
return 0; }```

2. Originally Posted by Null_Void
For the input of: 4.1, the output is supposed to be: 6. Please help, it seems that the output of the program that i wrote is just random.
Please post the actual code in [CODE] tags as instructed in the forum FAQ, NOT in fuzzy images!

3. Originally Posted by rstanley
Please post the actual code in [CODE] tags as instructed in the forum FAQ, NOT in fuzzy images!
Sorry, is it okay now?

4. On line 11 you are post incrementing number, then on the next line you are actually adding 3 to result2.
The result is not random, just not correct.

Could be done much simpler
Code:
```#include <stdio.h>

int main(void)
{
// Initialize all local variables to 0 or some other value
double number0 = 0.0;
int number = 0;

while (1) {

// scanf() needs error checking!
// Check the return value from scanf() and handle an error
scanf("%lf",&number0);

number = (int)number0;
if(number % 2 == 0) // 0 if even, 1 otherwise
{
number += 2;
}
else
{
number++;
}

printf("The nearest even integer that's greater than the entered number = %d. \n", number);
}
return 0;
}```

5. Thank you!

6. Also, try and not develop the habit to write code for non-terminating programs. If you use something like while(1), make sure to include a terminating condition inside the loop at least, although, it is always better to have the loop condition in the for/while statements itself.