C program to find even and odd numbers

In this article, you will learn to write a C program to find even and odd numbers. After reading the article you are able to check whether a number entered by the user is even or odd.

 

What are Even numbers?

A number that is evenly divisible by 2 is called an even number. That means when you will divide an even number by 2, the remainder will be 0.

For example: 2, 4, 8, …etc

 

What are the Odd numbers?

A number that is not evenly divisible by 2 is called an odd number. That means when you will divide an odd number by 2, the remainder will be 1.

For example: 1, 3, 7, …etc

 

C program to find even and odd numbers:

Before writing the C code to check whether a given number is even or odd let’s understand the problem statement and the examples. It helps you in writing the code.

Problem Statement: 

Given an integer num, the task is to check if the given number num is even or odd. If it is found to be even, then print “Number is Even”. Otherwise, print “Number is Odd”.

Example:

Input: data = 12
Output: Even

Input: data = 17
Output: Odd

 

 

Method 1: Using the Modulus Operator and if-else condition:

The easiest approach for beginners is to check whether a given number is even or odd by dividing the given number by 2. If the data is even, the remainder will be 0 otherwise 1.

Consider the below code for better understanding.

#include <stdio.h>

int main()
{
    int data;

    //Get input from the user
    printf("\n Enter any number: = ");
    scanf("%d", &data);

    //If number is divisible by 2 then 
    //it is a even number
    if((data % 2) == 0)
    {
        printf("\n Number is Even.\n\n");
    }
    else
    {
        printf("\n Number is Odd.\n\n");
    }
    
    return 0;
}

Output 1:

if else programming exercises

 

Output 2:

if else programming exercises

 

 

Method 2: Using Modulus Operator and ternary conditional operator:

Besides using the if-else statement you can also use the ternary conditional operator to check whether the given number is even or odd.

#include <stdio.h>

int main()
{
    int number;

    //Get input from the user
    printf("\n Enter any number: = ");
    scanf("%d", &number);

    //If number is divisible by 2 then
    //it is a even number
    (number%2) == 0 ? printf(" Even Number \n ") : printf(" Odd Number \n");

    return 0;
}

 

 

Method 3: Using the bit-wise operators:

Using the bit-wise operation you can also check that the given number is even or odd. You need to check the LSB bit of the given number; if it is set that means the number is odd otherwise even.

Now you are thinking about how you will check the LSB bit of the given number. Don’t worry I will explain. If the given integer number is ‘data’, you need to find the value of (data & 1). If the result is 1, then print “Number is Odd”. Otherwise, print “Number is Even”.

Example-1: suppose data = 4;

So,

The binary representation of 4 is 0000000000000100 (Assume int size is 2-byte)

The binary representation of 1 is 0000000000000001

——————————————————————-
Value of Bitwise AND operation of (0000000000000100 & 0000000000000001) is 0000000000000000

Since the result is 0. That means “controlling expression” of if statement is 0. So the body associated else will be executed and print the message number is even.

Example-2: suppose data = 7;

So,

The binary representation of 7 is 0000000000000101 (Assume int size is 2-byte)

The binary representation of 1 is 0000000000000001

——————————————————————-
Value of Bitwise AND operation of (0000000000000101 & 0000000000000001) is 0000000000000001

Since the result is 1. That means “controlling expression” of if statement is 1. So, the body associated with if statement will be executed and print the message “number is odd“.

 

#include <stdio.h>

int main()
{
    int data;

    //Get input from the user
    printf("Enter any number: = ");
    scanf("%d", &data);

    //Check LSB bit of the number
    if(data&1)
    {
        printf("Number id Odd \n ");
    }
    else
    {
        printf("Number id Even \n ");
    }

    return 0;
}

 

If you want you can remove if-else and use the ternary condition operator. Consider the below example code.

#include <stdio.h>

int main()
{
    int data;

    //Get input from the user
    printf("Enter any number: = ");
    scanf("%d", &data);

    //Check LSB bit of the data
    (data&1) ? printf("Number is Odd") : printf("Number is Even");

    return 0;
}

 

 

Method 4: Using the extra variable:

Here basic idea is to create and initialize an integer variable (isEven) with 1 and toggle its value alternately, n times (value of data). This integer variable work like an indicator If isEven is equal to 1 after n iteration, print “Even Number”. Otherwise, print “Odd Number”.

#include <stdio.h>


int main()
{
    int data;

    // Initialize a variable var
    int isEven = 1;

    //Get input from the user
    printf("Enter any number: = ");
    scanf("%d", &data);


    // Iterate till data
    for (int i = 1; i <= data; i++)
    {
        // Subtract temporary var from 1
        isEven = (1 - isEven);
    }

    //check even or odd number
    printf(isEven? "Even number": "Odd number");


    return 0;
}

Output:

Enter any number: = 7
Odd number

 

Recommended Articles for you:

One comment

Leave a Reply

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