Check value

C program to check whether a number is even or odd

C program to check whether a number is even or odd

In this tutorial, we will discuss a simple concept of C program to check whether a number is even or odd

In this program, we are going to learn about how to find  odd or even number from given number using if else statements in C programming language

 

what is even or odd

When the number is exactly divisible by 2, it is called as the even number eg – 2,4,6,8,10

if the number is not exactly divisible by 2, that is called the odd number – 1,3,5,7,9

 

Once This C program received the number it will check the given number either odd or even.

After the program received input from the user, it is stored in the variable of num. then program divides the value of num into 2 and displays the output

Program 1

Using modular operator

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    printf("Enter a integer value: ");
    scanf("%d",&num);
    //get input from the user and store the value in num variable.

    if(num%2==0)//if the number is divided by 2, the number is even
        printf("%d is a even number",num);
    else//otherwise the number is odd
        printf("%d is a odd number",num);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter an integer value: 34
34 is a even number

case 2

Enter an integer value: 65
65 is a odd number

when we use the modular operator, if it is the remainder appears as 0, it is called even number and if the remainder displays the balance as one, it is called as odd or uneven number

Using conditional operator

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    printf("Enter an integer number: ");
    scanf("%d",&num);
    (num%2==0) ? printf("%d is an even",num): printf("%d is an odd",num);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter an integer number: 34
34 is an even

case 2

Enter an integer value: 65
65 is an odd number

 

Using Bitwise operator

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    printf("Enter an integer number: ");
    scanf("%d",&num);

    if(num&1)
        printf("%d is a odd number",num);
    else
        printf("%d is a even number",num);
    getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter an integer number: 96
96 is an even

case 2

Enter an integer value: 27
27 is an odd number

 

 

Using Division operators

program 4

#include <stdio.h>
#include <stdlib.h>

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

When the above code is executed, it produces the following results

case 1

Enter an integer number: 54
54 is an even number

case 2

Enter an integer value: 39
39 is an odd number

Using shift operators

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    printf("Enter the number: ");
    scanf("%d",&num);

    if(((num>>1)<<1)==num ){
        printf("%d is even",num);
    }

    else{
        printf("%d is odd",num);
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter an integer number: 125
125 is odd

case 2

Enter an integer value: 200
200 is eve

 

The same program in other languages

C++ program to check whether a number is even or odd

Java program to check whether a number is even or odd

Python program to check whether a number is even or odd

 

Suggested for you

Operator in C language

Data type in C language

If statements in C language

scanf() printf() function in C language

 

 

Java code to check whether a number is even or odd
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago