C Program to Check Whether a Number is Even or Odd

by | Jan 8, 2023 | C, C Programs

Introduction

This program will check whether a number is even or odd.

An even number is the one that is exactly divisible by 2. On the other hand, an odd number is the one which is not exactly divisible by 2.

In this program, we will use this logic to check whether a number is even or odd.

To write this program, you should know about the following C programming concepts:

1. C programming operators

2. if….else statement

Program

#include<stdio.h>
int main()
{
int x;
printf("Enter a number:");
scanf("%d",&x);


if(x%2==0) //checking whether x when divided by 2 gives 0 as remainder or not
{
printf("%d is an even number",x); //statement will be printed if the above condition is true
}
else
{
printf("%d is a odd number",x); // if the condition is false then this statement gets printed
}
return 0;
}

 

Output

Explanation

‘x’ is a variable whose value will be entered by the user.

if(x%2==0) here we will check whether the remainder obtained by the division of x by 2 gives 0 or not. If it results true then the body of the ‘if’ statement will be executed.

If the above condition is false, then the body of the else part is executed.

In the output section, the value of ‘x’ is 9865247, which is an odd value, thus the body of the ‘else’ statement is executed.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.