Learn to check odd-even number in C++

In this article, you’ll learn how to check odd-even number in C++ programming without giving input from the keyboard or giving input from the keyboard.

Look at in the above diagram:

To check odd-even number we generally use modulo operator, we divide the given number with modulo operator and if get remainder == 0 then we say that the given number is even, otherwise, we say that the given number is odd.

 

Example 1: write a program in C++ language to check a number is odd- even without giving input from keyboard

#include<iostream>

using namespace std;

int main()

{

int num=52 ;

if(num%2==0)

cout<<num<<” is an Even number”<<endl;

else

cout<<num<<” is an Odd number”;

return 0;

}

 

The Output of this program:

52 is an Even number

 

Example 2: write a program in C++ language to check a number is odd-even, an input is given by user from the keyboard.

Solution:

#include<iostream>

using namespace std;

int main()

{

int num ;

cout<<“Enter an integer number: “;

cin>>num;

if(num%2==0)

cout<<num<<” is an Even number”<<endl;

else

cout<<num<<” is an Odd number”;

return 0;

}

 

The Output of this program:

Enter an integer number: 36

36 is an Even number

 

Example 3: Write a C++ Program to check odd-even number using the ternary operator, without input from the keyboard.

Solution:

#include<iostream>

using namespace std;

int main()

{

int num=78;

(num%2==0) ? cout<<num<<” is an Even number” :cout<<num<<” is an Odd number”;

return 0;

}

 

The output of this program:

78 is an Even number

Note: Ternary operator must write in a single statement;

The syntax of the ternary operator or conditional operator:

(condition) ? statement 1: statement 2;

Note: if the condition is true then statement 1 is executed and if the condition is false statement 2 is executed.

Example 4: Write a C++ Program to check odd-even number using ternary operator, with input from the keyboard

Solution:

#include<iostream>

using namespace std;

int main()

{

int num;

cout<<“Enter an integer number: “;

cin>>num;

(num%2==0) ? cout<<num<<” is an Even number” :cout<<num<<” is an Odd number”;

return 0;

}

 

 

The output of this program:

Enter an integer number: 57

57 is an Odd number

 

Example 4: Write a C++ Program to check odd-even number without using module (%) operator, without input from the keyboard.

#include<iostream>

using namespace std;

int main()

{

int num=69 ;

if(num &1)

cout<<num<<” is an Odd number”<<endl;

else

cout<<num<<” is an Even number”;

return 0;

}

 

The output of this program:

69 is an Odd number

Note: In the above program we use & (bitwise AND) operator, this operator works on the binary number. When we transform an even number into binary form then its last bit always 0 and binary of number 1 in the last bit is 1 then we get result 0 (false). since 0*1=0 it means else part is executed, on the other hand, if we transform an odd number into binary form then its last digit always 1 and it gives true and if part is executed.

Example 5: Write a C++ Program to check odd-even number without using module (%) operator, with input from the keyboard.

#include <stdio.h>

#include <conio.h>

int main()

{

int num;

printf(“Enter a integer number: “);

scanf(“%d”,&num);

if (num & 1)

printf(“%d is an Odd number “,num);

else

printf(“%d is an Even number”,num);

getch();

}

 

The Output of this program:

Enter an integer number: 96

96 is an Even number

Leave a Reply

Your email address will not be published. Required fields are marked *

itetrade.com