Q:

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

0

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.

All Answers

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

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.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now