Checking odd or even

This checks whether an integer is odd or even using the modulus % operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>

main()
{
int n;

printf("Enter an integer\n");
scanf("%d",&n);

if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");

return 0;
}

We can also check if an integer is odd or even by using the bitwise operator, as an integer in binary will always have a LSB of 1 if it’s odd and 0 if it’s even.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>

main()
{
int n;

printf("Enter an integer\n");
scanf("%d",&n);

if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");

return 0;
}
^