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


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

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.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. int num;
  8.  
  9. //Reading a number from user
  10. cout<<"Enter any number to check even or odd: ";
  11. cin>>num;
  12.  
  13. switch(num % 2)
  14. {
  15. //If n%2 == 0
  16. case 0: cout<<"Number is even";
  17. break;
  18.  
  19. //Else if n%2 == 1
  20. case 1: cout<<"Number is odd";
  21. break;
  22. }
  23. return 0;
  24. }
  25.  


Result

C++ language techstudy.org

     





Related Topics




Popular Posts