Avinash Singh C Programming C Program to Check Whether a Given Number is Even or Odd

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

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.

## 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 <stdio.h>

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 <stdio.h>

```
```

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.

I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E â€“ Vision Technocraft Pvt Ltd.

## C Programming Language for Microcontrollers: Getting StartedC Programming Language for Microcontrollers: Getting Started

Welcome to the world of microcontrollers and the C programming language! Microcontrollers are small, programmable devices that can be used in a wide range of applications, from home automation to

## if else Statement in C Programmingif else Statement in C Programming

In this post, we will learn with examples how to use if else statement in C programming. For any task in our C programs, which we give instructions, such as

## Advanced C Programming Language: Pointers and StructuresAdvanced C Programming Language: Pointers and Structures

Contents1 Introduction1.0.1 Table of Contents2 Pointers2.1 What are Pointers?2.2 Declaring and Initializing Pointers2.3 Dereferencing Pointers2.4 Pointers and Arrays3 Structures3.1 What are Structures?3.2 Declaring and Initializing Structures3.3 Accessing Structure Members3.4 Structures