# Thread: Even & odd number

1. ## Even & odd number

program with % operator
Code:
```#include <stdio.h>

int main(void)
{
int number;

printf("Enter an integer: ");
scanf("%d", &number);

if(number % 2 == 0)
printf("Even number is %d.", number);
else
printf("Odd number is%d.", number);

return 0;
}```
Enter an integer: 11
Odd number is 11.

Program with division operator

Code:
```#include <stdio.h>
int main(void)
{
int number;

printf("Enter an integer: ");
scanf("%d", &number);

if(number / 2 == 0)
printf("Even number is %d.", number);
else
printf("Odd number is %d.", number);

return 0;
}```
Enter an integer: 11
Odd number is 11.

Both program's give same output but they use different operator.

Which one programs is better and why this one is better 2. The second program will only ever recognize zero as even, and nothing else. That is because every division has two results, the quotient and the remainder. The "/" operator gives you the quotient and the "%" operator gives you the remainder. The remainder is what allows you to categorize the number as even or odd. You can do it with the quotient alone but it's a different operation:
Code:
```if ((number/2)*2 == number)
printf("Even number is %d.", number);
else
printf("Odd number is %d.", number);``` 3. number is an int. number / 2 will not give you a remainder . 4. Actually, that is the point, and I think Greaper knows that. If int gave a remainder, then number/2*2 == number would always be true, which is less than useful. Popular pages Recent additions #### Tags for this Thread

int, integer, number, number;, operator 