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
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
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
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
Suggested for you
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# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…