Check value

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

Find odd or even 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

Variable in C++ language

The operator 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

 

 

Find odd or even number using switch statements in C
5 ways to check whether the given integer is Even or Odd in Java
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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago