Friday, 4 April 2014

How to Check a Number is Even or Odd?

It is very simple to check that a given number is even or odd by dividing the number by 2. But it is not possible to check by division every number. So it is very easy that we can do it by writing a C Program with different methods.

/*Check Even or ODD with Modulus Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    if(numb%2==0)
    {
         printf("\nEven Number");
    else
         printf("\nOdd Number");
    }
getch();
}

/*Check Even or ODD with Bitwise Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    if(numb & 1==1)
    {
         printf("\nOdd Number");
    else
         printf("\nEven Number");
    }
getch();
}

/*Check Even or ODD with Conditional Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    numb%2==0 ? printf("\nEven") : printf("\nODD");
    return 0;
}

If you have programming skills then you can develop other methods of finding whether given number is even or odd. And if you have any queries do get back to me, I will respond to your queries within 24 hours with the positive solution.

No comments:

Post a Comment