Write C++ program to check even or odd number using switch case

Introduction

This program will read an integer number from user & check whether it is an even or odd number, using switch case statement in c++ programming language. I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

 

Logic to implement

  • Find the modulus of number dividing by 2.
  • Check the case values with 1 & 0.
  • In the case value 1, number will be odd and case value 0, number will be even.

 

#include <iostream>
 
using namespace std;
 
int main(){
 
    int num;
 
    //Reading a number from user
    cout<<"Enter any number to check even or odd: ";
    cin>>num;
 
     switch(num % 2)
    {
        //If n%2 == 0
        case 0: cout<<"Number is even";
                break;
 
        //Else if n%2 == 1
        case 1: cout<<"Number is odd";
                break;
    }
    return 0;
}
 

Result

Write C++ program to check even or odd number using switch case
Write C++ program to check even or odd number using switch case

Leave a Comment