Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter any number to check even or odd: 12

Number is even

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C++ program to check vowel or consonant usin... >>
<< Write C++ program to create simple calculator usin...