How to Check if a Given Number is Even or Odd in C Language

Check if a Given Number is Even or Odd in C Language

Introduction :

In programming world, finding a number is even or odd is a common task. Using the C programming language, you can easily check a given number is even or odd. You have to use the modulus operator (%) to perform this task. The modulus operator (%) returns the remainder of a division.

To find even number, you have to divide the number by two (2). If the remainder is zero (0), the number is an even number. If the remainder is one (1), the number is an odd number. In this article, I shall show you how to check a given number is even or odd in C programming language.

What is even or odd number :

An integer number which divisible by 2 without a remainder is called even number such as 2, 4, 6, 8, and so on. An integer number which divided by 2 leaves a remainder of 1 is called odd number such as 1, 3, 5, 7, and so on.

About the program :

This is a simple C program that checks the given number is even or odd. This program takes an integer input from the user. Then, calculate the number is even or odd using the modulus (%) operator. After that, displays the result on the console screen.

Explanation of the program :

First, you have to include stdio.h header file in the program. Then, declare an integer variable such as “given_number” which stores the input from the user. After that, ask the user to enter an integer number using printf() function. You can use scanf() function which reads the input number and store in “given_number” variable.

Now, calculate the input to check the number even or odd using modulus operator (%). Finally, display the result using printf() function. Here, you can use “if else” statement to print whether the number even or odd.

How run the program :

To run the program on your PC, you have to install VS Code. Then, create a new file like “even_odd.c” and paste the below code in the file. Now, save the file and run the program on your PC.

Source code of the program :

The following code is the source code of finding even or odd number in C programming language.

Output of the program :

After running the program, you can see the output like below image on your PC.

Output of finding even or odd number in C programming language

Conclusion :

At last, you have learned how to check a given number is even or odd program in the C programming language. Checking if a number is even or odd is a simple task which you can check using C language. Now, you can apply the same logic to solve more complex programs. Thank you for visiting my site.

Scroll to Top