106 Programming and Data Structures
case 2 :
printf (n Hours :%ld"Jtr$);
break;
case 3 :
printf (" Days: %ld",ds);
break;
case 4 :
printf ( Months: %ld",mon);
break;
case 5 :
printf ("n Seconds: %ld"^e);
break;
caseO :
printf (" Terminated by choice");
exitO;
break;
default:
printf (" Invalid Choice");
}
getchO;
I
OUTPUT;
[1] MINUTES
[2] HOURS
13] DAYS
14] MONTHS
[5] SECONDS
10] EXIT
Enter Your Choice: 4
Enter Years: 2
Months: 24
Explanation In this program the number of years are entered and according to user's choice swi tch
() case structure performs the operation.
5.23 Write a program to display the names of the days of a week.
# Include <stdlo.h>
znainO
{
in t d a y ,i;
Decision Statements 107
c lrs c r () i
printf ("Enter a number between lto7
scanf ("%d",(kday);
for (id ,i<’=day,'i++)
switch(i) I
case 1 :
printf(*nlst day of week is Sunday");
break;
case 2:
printf (u 2nd day of week is Monday");
break;
case 3:
printf ("n3rd day of week is Tuesday");
break;
case 4;
printf ( 4th day of week is Wednesday");
break;
case 5:
printf (" 5th day of week is Thursday");
break;
case 6:
printf (" 6th day of week is Friday");
break;
case 7:
printf (“ 7th day of week is Saturday");
break;
default:
printf (" Invalid day");
}
)
OUTPUT;
Enter a number between 1 to 7 :4
1st day of week is Sunday
2nd day of week is Monday
3rd day of week is Tuesday
4th day of week is Wednesday
Explanation In this program the sw itch () case structure is used as usual.
5.24 Write a program to perform following operations.
1.Display any numbers or stars on the screen by using for loop.
2. Display the menu containing the following
a) Whole screen, b) Half Screen, c) Top 3 lines d) Bottom 3 lines.
108 Programming and Data Structures
xnalnO
{
int i ,c ;
clrscr();
for (i=0,n<=700,n++)
printf ("%d",i);
printf ("nnttCLRAR SCREENMENU ");
printf (" l] Whole screenn");
printf ("tt2] Half Screenn");
printf (" t3] Top 3 Lines n");
printf ("tt4] Bottom 3 Lines ");
printf ("tt5] Exit n Enter Your Choice:");
scanf (“%d",8zc);
sxvitch(c)
I
case 1:
clrscti);
break;
case 2:
for (i=0;i<=189,'i++)
/
gotoxy(i,l);
printf Ct");
I
break;
case 3:
for (i=l,i<=99,i++)
I
gotoxy(i,l);
printf r n ;
}
break;
case 4:
for (i=l,mi<120,'i++)
{
gotoxy(ir21);
printf ("t");
}
default:
break;
/
getcheO;
I
Explanation In the above program the f o r loop is used for displaying numbers on the screen. The
screen will be covered with numbers. The screen as a whole or part on top or bottom portion can be
cleared by using sw itch () cases. While using switch () cases fo r loops are used. The programmer
can execute the program and see its effect by entering different choices.
Decision Statements 109
5.25 Write a program to display the following files of current directory.
1) .EXE FILES 2) .BAT FILES 3) .OBJ FILES 4) .BAR FILES. By using system dos command.
znainO
{
in t c ;
c l r s c r ();
printf ("n FILE LISTING MENU ");
printf ("n 1J.EXE");
printf ("n 2].BAT ");
printf ("n 3].obj ");
printf ("n 4] ,bak Enter Your Choice ");
scanf ("%d", &c);
switch (c)
{
case 1:
system("dir .exe");
break;
case 2:
system("dir .c");
break;
case 3:
system("dir .obj");
break;
case 4:
system ("dir .bak");
break;
default:
break;
I
getcheO;
}
Explanation In the above program menu is displayed. The user can give different choices. The effect
can be observed by selecting one of the choices. The user can view all. e xe or . b a t or . obj or . bak
files. The effect is same as dos ' d i r ' command. System () function is used to call the operating
system command.
5.26 Write a program to display number of days in calendar format of an entered month of
year 2001.
main()
{
int m,h,i=l,a, j,bel;
clrscr();
printf ("n Enter Month No. o f Year 2001:");
scanf ("%d”,&m);
/* Program for finding a day any month o f2001 V
szmtch(m)
110 Programming and Data Structures
{
case 1:
a=2;
;* 3 i;
break;
case 2:
«-5/
j*28;
break;
case 3:
am5;
break;
case 4:
ami}
j*30;
break;
case 5 :
a=3;
M l ;
break;
case 6:
a=6;
;=30;
break;
case 7 ;
<1=1;
j=31;
break;
case 8:
a*4;
j=31;
break;
case 9:
a*7;
j=30}
break;
case 10:
a* 2;
;=31;
break;
..................Content has been hidden....................

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