How to check Even or Odd Program in C (Three Examples)

CWC
5 Min Read

C programming can be a fun way to get our logical instincts going. As it uses simple language to let the compiler formulate results, there are a variety of easy programs that can be used at a beginner level to form the basis of future advancements. Numbers are vital, and so trying the program to come up with a way to distinguish between even and odd numbers can be interesting. It can be done through a few approaches, such as by using the modulus operator, bitwise operator, and conditional operator. Using these operators can help define and store values for further operations and commands. Here in this post, we are going to check the Odd or Even Program in C.

We are going to learn to use a simple code in C language to check the Odd or Even numbers. This is quite a useful tutorial to know about the Even or Odd program in C. This is very easy to understand, and it is used in almost all the programming languages to find the Odd or Even. In this example, we will learn the use of the modulus operator which is used to find the Even or Odd Program in C.

Odd or Even Program in C

Even or Odd Program in C – Using Conditional Operators in C programming

The conditional operator == is used to check if any of the two operands defined are equal. The condition becomes true when the desired operands are equal.


#include 
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}

Input an integer: 8
8 is even. 
Input an integer: -3
-3 is odd.

Using Modulus Operator in C programming

Modulus operators act as a way to divide and then come up with a solution. The operator % is designed to divide the numbers into half and find the result, equal to the requested command. There is also a conditional operator to ensure that the values that come can be equated to the values of being Odd or Even Program in C.


#include 
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}

Enter an integer: 6
6 is even. 
Enter an integer: -5
-5 is odd.

Using Bitwise Operator

Bits are usually defined as either 0 or 1. And the only value they can tell is either a number is 1 or 0. Therefore, using the AND command to see if any of the numbers are even or odd, may usually tell the result of odd numbers as being 1. It can be rather complicated at times and needs a grip over the way bits to work.


/* Program to check if number is even or odd
* using bitwise operator
*/
#include
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n & 1)
printf("%d is an odd number", n);
else
printf("%d is an even number", n);
return 0;
}

Enter an integer: 77
77 is odd. 
Enter an integer: 44
44 is even. 

One of the hardest ways in using a bitwise operator is the way conditions work in it. The AND command is vital for the compiler to understand if both operands are non-zero or not. If they are, the condition can be deemed true. Likewise, OR operators are also using the same conditions to come up with a true non-zero operand.

The C programming language has two types of variables: integer and character variables. Integer variables can be declared as either odd or even. Character variables can be declared as either lowercase or uppercase. Odd integer variables always have an even number of digits. Even integer variables always have an odd number of digits. You cannot assign values to odd variables and even variables cannot be assigned values. Here are two programs that will output the type of variable that a certain variable represents.

Share This Article
Leave a comment

Leave a Reply

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

English
Exit mobile version