Check value

C code to 5 ways to check whether the given integer is Even or Odd

C code to 5 ways to check whether the given integer is Even or Odd

In this tutorial, we will discuss the concept of C code to 5 ways to check whether the given integer is Even or Odd

In this post, we are going to learn how to check whether the given integer is even or odd using 5 different ways in the C programming language.

Check odd even using operator

Using modular operator

Program 1

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

int main()
{
    int num;
    printf("Enter a integer number: ");
    scanf("%d",&num);//takes input from the user
    //using modular operator
    if(num%2==0){
  printf("%d is a Even number",num);
}else{
 printf("%d is a Odd number",num);
}
getch();
    return 0;
}

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

Case 1

Enter a integer number : 25
25 is a odd number

Case 2

Enter a integer number : 36
36 is a even number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using modular operator

 

Using division operator

Program 2

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

int main()
{
    int num;
    printf("Enter a integer number: ");
    scanf("%d",&num);//takes input from the user
    //using division operator
    if((num/2)*2==num){
  printf("%d is a Even number",num);
}else{
 printf("%d is a Odd number",num);
}
getch();
    return 0;
}

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

Case 1

Enter a integer number : 38
38 is a even number

Case 2

Enter a integer number : 41
41 is a odd number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using division operator

 

Using bitwise AND operator

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

int main()
{
    int num;
    printf("Enter the integer number: ");
    scanf("%d",&num);//takes input from the user
    //using Bitwise AND operator
    if((num&1)==0){
  printf("%d is a Even number",num);
}else{
 printf("%d is a Odd number",num);
}
getch();
    return 0;
}

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

Case 1

Enter a integer number : 100
100 is a even number

Case 2

Enter a integer number : 111
111 is a odd number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using betwise operator

Using shift operator

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

int main()
{
    int num;
    printf("Enter a integer number for num: ");
    scanf("%d",&num);//takes input from the user
    //using shift operator
    if(((num>>1)<<1)==num){
  printf("%d is a Even number",num);
}else{
 printf("%d is a Odd number",num);
}
getch();
    return 0;
}

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

Case 1

Enter a integer number : 246
246 is a even number

Case 2

Enter a integer number : 201
201 is a odd number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using shift operator

Using ternary operator

Program 5

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

int main()
{
    int num;
    printf("Enter a integer number for num: ");
    scanf("%d",&num);//takes input from the user
    //using ternary operator
    (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 results

case 1

Enter a integer number for num: 121
121 is odd

case 2

Enter a integer number for num: 200
200 is even

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using ternary operator

 

Suggested for you

Data type in C language

Variable in C language

Operator in C language

Input output function in C

 

Similar post

Program to find whether a Number is Prime or Not in C++

Program to find whether a Number is Prime or Not in C

Program to find whether a Number is Prime or Not in Java

Program to find whether a Number is Prime or Not in Python

 

C++ program to find first n prime numbers

Java program to find first n prime numbers

C program to find first n prime numbers

 

C code to 5 ways to check whether the given integer is Even or Odd

Java code to 5 ways to check whether the given integer is Even or Odd

C++ code to 5 ways to check whether the given integer is Even or Odd

Python code to 5 ways to check whether the given integer is Even or Odd

 

 

5 ways to check whether the given integer is Even or Odd in Java
C++ code to 5 ways to check whether the given integer 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