Even or Odd

C program to determine whether the number entered by user is even or odd.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
	int number;

    printf("Enter a number\t: ");
    scanf("%d",&number);

    if((number%2)==0)
    {
        printf("\nEntered number is an even number");
    }
    else
    {
        printf("\nEntered number is an odd Number");
    }

    getch();
}

Output

********** Run1 ********** 

Enter a number  : 3

Entered number is an odd Number


********** Run2 ********** 

Enter a number  : 4

Entered number is an even number

Explanation

In above problem, there are two cases, either the entered number is odd or it is even.

A variable 'number' is used to store the value of number entered by user. if-else construct is used to accomplish the above task.

if(number % 2 == 0)
The above statement checks whether the number is an even number or not. If remainder of 'number' when divided by 2 equals 0 then, the above condition evaluates to true and if block is executed.

printf("\nEntered number is an even number");
This statement prints that 'Entered number is an even number' and the control comes out of the entire if-else construct.

If above condition evaluates to false, then its obvious that the entered number is odd. So there is no need to check any condition. Foe this purpose, else block is used.
else
{
printf("\nEntered number is an odd Number");
}
This statement prints that 'Entered number is an odd Number'.