ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C program to check whether number is EVEN or ODD using switch

This program will read an integer number and check whether it is an EVEN or ODD number using switch case statement in c programming language.

Logic to implement

  1. Find the modulus of number dividing by 2 (as we know that EVEN number’s modulus is 0 and ODD number’s modulus is 1).
  2. Check the case values with 0 and 1.
  3. In the case value 0, number will be EVEN and case value 1, number will be ODD.

Check EVEN or ODD program using switch

/*C program to check whether number is EVEN or ODD using switch.*/
 
#include <stdio.h>
 
int main()
{
    int number;
     
    printf("Enter a positive integer number: ");
    scanf("%d",&number);
     
    switch(number%2) //this will return either 0 or 1
    {
        case 0:
            printf("%d is an EVEN number.\n",number);
            break;
        case 1:
            printf("%d is an ODD number.\n",number);
            break;
    }
     
    return 0;
}

Output

First run:
Enter a positive integer number: 10 
10 is an EVEN number. 

Second run:
Enter a positive integer number: 11 
11 is an ODD number.

C Switch Case Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.