C++ program to find the odd or even number using switch statements
- Home
- Check value
- C++ program to find the odd or even number using switch statements
- On
- By
- 0 Comment
- Categories: Check value, Find elements
C++ program to find the odd or even number using switch statements
C++ program to find the odd or even number using switch statements
In this tutorial, we will discuss the concept of the C++ program to find an odd or even number using switch statements.
In this post, we are going to learn how to find odd or even number using switch statements in C++ programming language
when you divide a number by two and if the balance is zero, it is an even number
when you divided a number by two and if the balance is one, it is an odd number
Example of even number 2,4,6,8,…..
Example of odd number 1,3,5,7,…..
Here we will use a modular operator to display odd or even number of the given number
if n%2==0 n is an even number
if n%2==1 n is an odd number
C++ program to find odd or even number
Find the Even number – Standard method
The program will check and display the even numbers from the given number using switch statements
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num1=452; // int num2=591; switch(num1%2){ //this will return either 0 or 1 case 0: cout<<num1<<" is a even number"; break; case 1: cout<<num2<<" is a odd number"; } getch(); return 0; }
When the above code is executed, it produces the following results
452 is an even number
Find the Odd number – Standard method
The program will check and display the odd numbers from the given number using switch statements
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { //int num1=452; int num2=591; switch(num2%2){ //this will return either 0 or 1 case 0: cout<<num1<<" is a even number"; break; case 1: cout<<num2<<" is a odd number"; } getch(); return 0; }
When the above code is executed, it produces the following results
591 is an odd number
Find the odd or Even number – Entered by user
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 switch statements
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int num; cout<<"Enter the number\n"; cin>>num; switch(num%2){//this will return either 0 or 1 case 0: cout<<num<<" is a even number"; break; case 1: cout<<num<<" is a odd number"; } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the number 231 231 is an odd numbere
Case 2
Enter the number 1000 1000 is an even number
Approach
- The modular operator uses to find the modulus of the number dividing by 2.
- Switch statements return either 0 or 1.
- Then, check the case value with 0 or 1.
- if the case value is 0, the number will be even and case value 1, the number will be odd.
Suggested for you
Datatype in C++ language
Switch statements in C++ language
Similar post
Java program to find odd or even number using switch statements
C program to find odd or even number using switch statements
C program to find the number is odd or even with label using loops
C++ program to find the number is odd or even with label using loops
Python program to find the number is odd or even with label using loops
Java program to calculate average of odd or even numbers
C program to calculate average of odd or even numbers
C++ program to calculate average of odd or even numbers
Java program to calculate sum of odd or even numbers
C program to calculate sum of odd or even numbers of an array
C++ program to calculate sum of odd or even numbers in an array
Python program to calculate sum of odd or even numbers in a list
Java program to check odd and even using recursion
C program to check odd and even using recursion
C++ program to check odd and even using recursion
Python program to check odd and even using recursion
Python program to check odd and even using function
Java program to check odd and even using Method
C program to check odd and even using function