0% found this document useful (0 votes)
37 views

Using C-The Given Number Is Even or Odd

The program takes a number as input, uses the modulo (%) operator to check if the number is evenly divisible by 2, and prints whether the number is even or odd. It explains with examples that for an even number like 4, 4%2 equals 0, while for an odd number like 7, 7%2 does not equal 0.

Uploaded by

vidhyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Using C-The Given Number Is Even or Odd

The program takes a number as input, uses the modulo (%) operator to check if the number is evenly divisible by 2, and prints whether the number is even or odd. It explains with examples that for an even number like 4, 4%2 equals 0, while for an odd number like 7, 7%2 does not equal 0.

Uploaded by

vidhyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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

Program:
#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.

You might also like