Even and odd printing using & operator and loop



#include <stdio.h>
int main()
{
    int x;
    for (x = 0; x <= 10; x++)
    {
        if (x & 1)
        {
            printf("%d - odd\n", x);
        }
        else
        {
            printf("%d - even\n", x);
        }
    }
    return 0;
}


Output : 

0 - even
1 - odd
2 - even
3 - odd
4 - even
5 - odd
6 - even
7 - odd
8 - even
9 - odd
10 - even

0 Comments