C Programming

# How to Check If a Number Is Even in C

In this article, we will come to know how to use the C language to analyze if a particular integer is even or odd. The term “even number” refers to an integer value that would be completely divided by 2. We would evaluate if an integer is even or odd by using the modulus (%) operator in programming. When the modulus 2 of every number exceeds 0, the value is even. Whereas, if it does not exceed 0, then the value is odd.

## Use Modulus Operator (%) to Determine Integer Is Even or Odd

In this code, we will apply the modulus (%) operator to specify whether a value provided by the user would be even or odd:

#include <stdio.h>

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(num % 2 == 0)
printf("%d is even number.", n);
else
printf("%d is odd number.", n);
return 0;
}

First, we just include the header file <stdio.h>; this header file handles the input and output functions. In the next step, we define the main() function. Inside the body of the main() function, we initialize a variable “n”, and it stores the integer. Here, we call the printf() function, so it prints the statement “Enter a number” to get the number from the user which he wants to know whether it is an even or odd number.

Further, we employ the scanf() function. It is used to read the configured data. It stores the entered number. In addition, we apply the if-else condition to check whether the entered number is even or not. We utilize the (%) modulus operator to evaluate if the entered number is perfectly divisible by 2 or not. Here, the test statement (number % 2 == 0) responds as 1 if the entered integer is completely divided by 2. This indicates that the integer would be even.

The integer is odd if the test condition responds to 0 (false). In this way, we decide whether the required number is even or odd.

## Use Bitwise Operator (&) to Determine Whether an Integer Is Even or Odd

To use the bitwise (&) operator, we would analyze whether a user-defined value is even or odd in this code. The required variables will be created and defined first. Then, the user will be asked to provide an integer value. We’ll employ the bitwise (&) operator eventually in the code to see if the user-defined integer is even or odd. Next, we’ll subsequently show the message whether the value is even or odd.

#include<stdio.h>

int main() {
int n;
printf("Enter a number: ");
scanf("%d",&n);
if ( n& 1)
printf("%d is odd number", n);
else
printf("%d is even number", n);
return 0;
}

At the beginning of the program, we introduce the <stdio.h> header file. This library provides the input and output functionalities. The main() function is called in the next moment. We create a variable “n”, which holds the integer, within the body of the main() method. The printf() function is defined here, and it displays the line “Enter a number” just to obtain any number from the user and decide if it is even or odd. We have been using the scanf() method. It is applied to access the data that has been specified. It retains the number we provided.

Furthermore, an if-else statement is utilized to evaluate if the entered value is even or odd. This can be done by using the bitwise operator (&). A bitwise operator manipulates discrete components in data bits or decimal values to execute bitwise operations. In transmission layers, where specific bits in the header related to the content represent crucial information, bitwise operators are utilized. Then to terminate the program, we apply the return 0 command.

## Use the Ternary Operator to Indicate the Even or Odd of the Number

We can also check if the entered number is even or odd by using the ternary operator. Rather than using the if…else expression, we employ the ternary operator (?):

#include <stdio.h>

int main()
{
int nbr;
scanf("%d", &nbr);
(nbr % 2 == 0) ?printf("%d is even.", nbr) : printf("%d is odd.", nbr);
return 0;
}

After introducing the required library <stdio.h>. We call the main() function. Now, the variable “nbr” is initialized to store the number. In addition, we employ the printf() function. Here, the user can provide an integer value, and that number is assigned to “nbr”. Further, we utilize the scanf() method, and it always gets the formatted information. The entered number is passed as a parameter to this function.

Now, we apply the modulus operator (%) to determine whether the entered number is even or odd. So, if (nbr % 2 == 0) returns true, then it shows the specified number is even; otherwise not. Meanwhile, we also use the ternary operator (?). The conditional operator is sometimes referred to as the ternary operator “?:”. It’s comparable to an if-else expression in that it utilizes the same logic. Still, this operator uses less storage and makes it easier to create if-else expressions in the shortest period. Because they require three operands for configuration, these are known as ternary operators.

It’s being utilized to run a program according to the outcome of a binary status. It works similarly to a method in that it accepts a binary value as input and provides a result. This operator enhances efficiency while reducing code lines. In the end, we use the return 0 statement.

## Conclusion

This article discussed the strategies for determining whether the provided number is even or odd. The three techniques used to evaluate if an integer is even or odd include the modulus (%) operator, the bitwise operator (&), and the ternary operator. Check the other Linux Hint articles for more tips and tutorials.