program to check whether the given number is even or odd.

29 Dec 2022 Balmiki Kumar 0 C Programming

Program to check whether the given number is even or odd.

A program to check whether a given number is even or odd is a computer program that prompts the user to enter an integer and then determines whether the entered integer is even or odd.

In mathematics, an even number is an integer that is divisible by 2 without a remainder, while an odd number is an integer that is not divisible by 2 without a remainder. The program uses the modulus operator % to check whether the entered number is divisible by 2. If the remainder is 0, the number is even, and if the remainder is 1, the number is odd.

The purpose of this program is to automate the task of determining whether a number is even or odd, which can be useful in various mathematical, scientific, or programming applications.

Program 01: Program to Check Whether a Given Number is Even or odd

#include<stdio.h>
int main() {
 int a;
 printf("Enter a: \n");
 scanf("%d", &a);
 /* logic */
 if (a % 2 == 0) {
 printf("The given number is EVEN\n");
 }
 else {
 printf("The given number is ODD\n");
 }
 return 0;
}

Output:

Enter a: 2
The given number is EVEN 

Explanation with examples:

Example 1:

If entered number is an even number
 Let value of 'a' entered is 4
 if(a%2==0) then a is an even number, else odd.
 i.e. if(4%2==0) then 4 is an even number, else odd.
To check whether 4 is even or odd, we need to calculate (4%2).
/* % (modulus) implies remainder value. */
/* Therefore if the remainder obtained when 4 is divided by 2 is 0, then 4 is even. */
 4%2==0 is true
 Thus 4 is an even number.

Example 2:

If entered number is an odd number.
 Let value of 'a' entered is 7
 if(a%2==0) then a is an even number, else odd.
 i.e. if(7%2==0) then 4 is an even number, else odd.
To check whether 7 is even or odd, we need to calculate (7%2).
 7%2==0 is false /* 7%2==1 condition fails and else part is executed */
 Thus 7 is an odd number.

 

BY: Balmiki Kumar

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.