Odd Even

Code to Check whether the number is odd or even using operators in C++

Code to Check whether the number is odd or even using operators in C++

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

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

 

Program to Check whether the number is odd or even

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

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num;//variable declaration
    cout<<"Enter a integer to check odd or even\n";
//The program ask input from the user
    cin>>num;
//input value is stored in variable num

    if(num%2==0){//using modular operator
        cout<<num<<" is even";
    }
    else{cout<<num<<" is odd";}
    getch();
    return 0;
}

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

case 1

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

case 2

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

 

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

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num;//variable declaration
    cout<<"Enter a integer to check odd or even\n";
//The program ask input from the user
    cin>>num;
//input value is stored in variable num

    if((num/2)*2==num){//using divisional operator
        cout<<num<<" is even";
    }
    else{cout<<num<<" is odd";}
    getch();
    return 0;
}

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

case 1

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

case 2

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

 

 

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

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num;//variable declaration
    cout<<"Enter a integer to check odd or even\n";
//The program ask input from the user
    cin>>num;
//input value is stored in variable num

    if((num & 1)==0){//using bit wise AND operator
        cout<<num<<" is even number";
    }
    else{cout<<num<<" is odd number";}
    getch();
    return 0;
}

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

case 1

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

case 2

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

 

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

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num;//variable declaration
    cout<<"Enter a integer to check odd or even\n";
//The program ask input from the user
    cin>>num;
//input value is stored in variable num

    (num%2==0)? cout<<num<<" is even":cout<<num<<" is odd"; //using ternary operator
    getch();
    return 0;
}

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

case 1

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

case 2

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

 

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

Program 5

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num;//variable declaration
    cout<<"Enter a number to check even or odd\n";
//The program ask input from the user
    cin>>num;
//input value is stored in variable num
    if((num>>1)<<1==num)  //using shift operator
        cout<<num<<" is Even number";
        else
         cout<<num<<" is Odd number";
         getch();
    return 0;
}

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

case 1

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

case 2

Enter a integer to check odd or even
46
46 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.