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).
Check the case values with 0 and 1.
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.
Logic to implement
Check EVEN or ODD program using switch
Output
need an explanation for this answer? contact us directly to get an explanation for this answer