As a beginner in programming, one of the first things you will learn is how to check whether a given number is even or odd. In this article, we will learn how to write a C program to check whether a given number is even or odd. We will start by discussing the concept of even and odd numbers, and then we will move on to writing the C program.

You can also read: Google Docs Vs. Microsoft Word: The Ultimate Showdown!

Contents

## What are Even and Odd Numbers?

In mathematics, a number is said to be even if it is divisible by 2. On the other hand, a number is said to be odd if it is not divisible by 2. In simple words, if a number is divisible by 2 without leaving a remainder, it is even, and if not, it is odd.

For example, 4 is an even number because it is divisible by 2, while 5 is an odd number because it is not divisible by 2.

## Writing a C Program to Check Whether a Given Number is Even or Odd

To check whether a given number is even or odd, we will use the modulo operator `%`

in our C program. The modulo operator returns the remainder of the division of two numbers.

Here’s the C program to check whether a given number is even or odd:

` ````
```#include
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.", num);
} else {
printf("%d is odd.", num);
}
return 0;
}

Let’s break down the above program into smaller parts to better understand how it works:

### Include Header Files

` ````
```#include

The above line of code includes the standard input/output library in our program, which provides functions for input and output operations.

### Define the Main Function

` ````
```int main() {
// Code goes here
return 0;
}

The `main()`

function is the starting point of our program. It is a mandatory function in every C program.

### Declare a Variable

` ````
```int num;

The above line declares a variable `num`

of type `int`

, which will store the number entered by the user.

### Prompt the User for Input

` ````
```printf("Enter an integer: ");
scanf("%d", &num);

The above code prompts the user to enter an integer and reads the input entered by the user into the `num`

variable using the `scanf()`

function.

### Check Whether the Number is Even or Odd

` ````
```if (num % 2 == 0) {
printf("%d is even.", num);
} else {
printf("%d is odd.", num);
}

The above code checks whether the number entered by the user is even or odd using the modulo operator `%`

. If the remainder of the division of `num`

by 2 is 0, it means that `num`

is even, and the program prints the message `num is even.`

Otherwise, it means that `num`

is odd, and the program prints the message `num is odd.`

### Return Statement

` ````
```return 0;

The `return`

statement in the `main()`

function terminates the program and returns a value of 0 to the operating system, indicating that the program ran successfully.

## Conclusion

In this article, we learned how to write a C program to check whether a given number is even or odd. We started by discussing the concept of even and odd numbers and then went on to write the C program step by

A: A C program is a set of instructions that a computer can execute written in the C programming language.

A: The modulo operator % in C returns the remainder of the division of two numbers.

A: In mathematics, a number is said to be even if it is divisible by 2.

A: In mathematics, a number is said to be odd if it is not divisible by 2.

A: The scanf() function in the C program is used to read input entered by the user.

A: The printf() function in the C program is used to print output to the console.

A: Yes, a negative number can be even or odd. The sign of a number does not affect whether it is even or odd.

A: No, a decimal number cannot be even or odd. The concept of even and odd numbers only applies to integers.

A: No, the C program to check whether a number is even or odd cannot be used to check whether a number is prime. Checking whether a number is prime requires a different algorithm.

A: No, it is not necessary to include the standard input/output library in the C program to check whether a number is even or odd, but it is good practice to do so as it provides functions for input and output operations.

You can also visit my HindiÂ YouTube Channel.