Decision Statements 113
clrscr () ;
printf (" Enter a number:");
scanf ("% d",&cx);
printf ("n Conversion o f Decimal to Hexadecimal Number n");
fo r (;;)
i
if (x==0)
exit(l);
z=x%16;
x=x/16;
gotoxy(y~,5);
switch(z)
I
case 10:
printf ("A/f);
break;
case 11:
printf ("%c"/B');
break;
case 12:
printf (“%c","C");
break;
case 13:
printf ("D");
break;
case 14:
printf ("E");
break;
case 15:
printf (“F");
break;
default:
printf ("%d",z);
I
QUTFUT;
Enter a number: 31
Conversion of Decimal to Hexadecimal Number
IF
Explanation In the above program an infinite f o r loop is used. In the f o r loop switch () case
statement is used for printing the characters A to F when remainder values are greater than 9. When
entered number (x) becomes zero, the i f terminates the program.
5.9 NESTED switch () CASE
C supports the nested swi tc h () statements. The inner swi tc h () can be a part of an outer swi tch ().
The inner and outer swi tc h () case constants may be same. No conflict arises even if they are same.
Few examples are given below based on the nested swi tch () statements.
114 Programming and Data Structures
S iT W -ifr? a program to detect whether the entered number is even or odd. Use nested
"' i Uh 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.
Decision Statements 115
5.29 Write a program to count number of Is, Os,blank spaces and others using
nested switch () statement.
# include <stdio.h># include <conio.h>
void main ()
{
s ta t ic in t x,s,a,z,o;
char tx t [20];
c lrs cr {) ;
printf (“nEnter Numbers :");
gets(txt);
while (txt[x]!-'0’)
(
switch(txt[x])
{
case ' ' :
s++;
break;
default;
switch(txt[x])
I
case '1':
a++;
break;
case 'O':
z++;
break;
default:
0++/
/
x++;
}
printf ("n Total Spaces: %d", s);
printf ("n Total Is : %d",a);
printf ("n Total Os : %d",z);
printf ("n Others :% d,o);
printf ("n String Length: %d",s+a+z+o);
OUTPUT:
Enter Numbers : 1110022 222
Total Spaces : 1
Totalis :3
Total Os : 2
Others : 5
String Length:ll
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.89.18