Translate

Labels

Wednesday 20 March 2013

ACCEPT A MONTH NUMBER AND DISPLAY THE MAXIMUM NUMBER OF DAYS IN THE MONTH.

#include<stdio.h>
#include<conio.h>
void main()
{
int month, days;
clrscr();
printf("Enter the month number \n");
scanf("%d",month);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days=31;
break;
case 4:
case 6:
case 9:
case 11:days=30;
break;
case 2:days=28;
break;
default:printf("Not a valid month number");
}
printf("Maximum no of days in the month %d=%d",month,days);
getch();
}
OUTPUT:
First run:
Enter month number
4
Maximum no of days in the month 4=30
Second run:
Enter month number

Maximum no of days in month 2=28

EXPLANATION
In this month and days are declared to be variables of int type.The variables month is to collect the month number (input) of  a year (1-12) and days is to collect the number of days in the given month.The variable month forms the expression of the switch structure used in the program and it evaluates to an integer constant.

During the course, of the execution of the program month value is accepted.Within the switch structure the value of the month is checked against 1,3,5,7,8,10,12 in a serial manner.If the value of the expression matches with any of these, then the variable days is assigned 31.
This segment of the structure is equivalent to the following if statement.

If((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
days=31;

Similarly if the value of the expression matches with any of the values 4,6,9 and 11 then days is assigned 30.This segment of the switch structure is equivalent to the following if statement.

If((month==4)||(month==6)||(month==9)||(month==11))
days=30;

If the values of the expression is 2, then days is assigned 28.If none of the labels match the values of the expression, then the default option is executed,After each of the above cases the control is transferred to the statement after the closing brace of the switch statement.

No comments: