Check value

C++ program to find the odd or even number using switch statements

C++ program to find 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago