114 Programming and Data Structures
S iT W -ifr? a program to detect whether the entered number is even or odd. Use nested
"' i U’h 0 case statements.
# include <stdio.h>
# include <eonio.h>
void main ()
{
Int x,y;
clrscr ();
printf ("n£nter a Number:");
scanf (“%d",&tx);
suritch(x)
f
caseO:
printf ("n Number is Even.");
break;
cctse 1 :
■printf ("Xn Number is Odd.
break;
default:
y=x%2;
switch(y)
{
case 0:
printf ("tt Number is Even.");
break;
default:
printf ("n Number is Odd.");
)
i
getcheO;
)
O UTPUT;
Enter a Number: 5
Number is Odd.
Explanation In the above cited program the first sw itch ( ) statement is used for displaying the
message such as even or odd number when the entered numbers are 0 and 1 respectively.
When the number is other than 0 and 1, its remainder is calculated and stored in the variable ' y '.
The variable 'y ' is used in the inner swi tch () statement. If the remainder is 'O' the message displayed
will be “Number is Even" otherwise for non-zero "Number is Odd". Here, the constants used for inner and
outer switch statements are the same.