Site icon Codeforcoding

C program to check whether a number is even or odd

Python program to check a number is even or odd using function

Python program to check 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

 

Similar post

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find 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
Exit mobile version