write a program to display the name of month of the year using switch case
Answers
Explanation:
Logic to print number of days in month using switch... case
Input month number from user. ...
Switch the value of month i.e. switch(month) and match with cases.
There can be 12 possible values (choices) of month i.e. from 1 to 12 . ...
Print 31 for case 1, 3, 5, 7, 8, 10, 12 .
Print 30 for case 4, 6, 9, 11 .
Answer:
/**
* C program to print number of days in a month using switch case
*/
#include <stdio.h>
int main()
{
int month;
/* Input month number from user */
printf("Enter month number(1-12): ");
scanf("%d", &month);
switch(month)
{
case 1:
printf("31 days");
break;
case 2:
printf("28/29 days");
break;
case 3:
printf("31 days");
break;
case 4:
printf("30 days");
break;
case 5:
printf("31 days");
break;
case 6:
printf("30 days");
break;
case 7:
printf("31 days");
break;
case 8:
printf("31 days");
break;
case 9:
printf("30 days");
break;
case 10:
printf("31 days");
break;
case 11:
printf("30 days");
break;
case 12:
printf("31 days");
break;
default:
printf("Invalid input! Please enter month number between 1-12");
}
return 0;
}