Odd Even

C Check whether the number is odd or even using operators

C Check whether the number is odd or even using operators

In this tutorial, we will discuss the Program to C Check whether the number is odd or even using operators

In this post, we are going to learn how to check whether the given number is odd or even number using operator in C language

C Check whether the number is odd or even

Check whether the number is odd or even – using modular operator

Program 1

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

int main()
{
    int num;//variable declaration
    printf("Enter a integer to check odd or even\n");
//the program Asks input from the user
    scanf("%d",&num);
//the integer entered by the user and it is stored in variable num

    if(num%2==0){
        printf("%d is even",num);
    }
    else{printf("%d is even",num);}
    getch();
    return 0;
}

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

case 1

Enter a integer to check odd or even
3500
3500 is even

case 2

Enter a integer to check odd or even
2345
2345 is odd

 

Check whether the number is odd or even – using divisional operator

Program 2

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;//variable declaration
    printf("Enter a integer to check odd or even\n");
//the program Asks input from the user
   scanf("%d",&num);
//the integer entered by the user and it is stored in variable num

    if((num/2)*2==num){
       printf("%d is even number",num);
    }
    else{printf("%d is odd number",num);}
    getch();
    return 0;
}

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

case 1

Enter a integer to check odd or even
3506
3506 is even number

case 2

Enter a integer to check odd or even
999
999 is odd number

 

Check whether the number is odd or even – using ternary operator

Program 3

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;//variable declaration
    printf("Enter a integer to check odd or even\n");
//the program Asks input from the user
   scanf("%d",&num);
//the integer entered by the user and it is stored in variable num

     (num%2==0)? printf("%d is even",num):printf("%d is odd",num);
    getch();
    return 0;
}

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

case 1

Enter a integer to check odd or even
5678
5678 is even

case 2

Enter a integer to check odd or even
6767
6767 is odd

 

Check whether the number is odd or even – using bit wise operator

Program 4

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;//variable declaration
    printf("Enter a integer to check odd or even\n");
//the program Asks input from the user
   scanf("%d",&num);
//the integer entered by the user and it is stored in variable num
if((num & 1)==0){
      printf("%d is even number",num);
    }
    else{printf("%d is odd number",num);}

    getch();
    return 0;
}

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

case 1

Enter a integer to check odd or even
4
4 is even number

case 2

Enter a integer to check odd or even
5
5 is odd number

Check whether the number is odd or even – using shift operator

Program 5

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

int main()
{
    int num;//variable declaration
    printf("Enter a number to check even or odd\n");
//the program Asks input from the user
    scanf("%d",&num);
//the integer entered by the user and it is stored in variable num
    if((num>>1)<<1==num)
        printf("%d is Even number",num);
        else
         printf("%d is Odd number",num);
         getch();
    return 0;
}

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

Case 1

Enter a number to check even or odd
4321
4321 is Odd number

Case 2

Enter a number to check even or odd
4326
4326 is Even number

 

Suggested for you

Data type in C language

Variable in C language

Operator in C language

While loop in C language

 

Similar post

Java Program to check whether the number is odd or even using operators

C Program to check whether the number is odd or even using operators

C++ Program to check whether the number is odd or even using operators

Python Program to check whether the number is odd or even using operators

Karmehavannan

Share
Published by
Karmehavannan
Tags: C language

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.