Task 5-Determining the number of days by months - solution
The current year and the ordinal number of the month are loaded first. The year is needed to determine the number of days if it is the month of February, to determine whether the year is a leap year or not.
The switch command first classifies months that have 31, then those that have 30, and finally month 2, whose number of days is the most difficult to determine. The feature of the switch command is used here, that once it enters the appropriate case, it does not interrupt the execution until it encounters the "break" command. The "break" command is therefore set only when all cases with the same number of days are listed.
The condition for a leap year includes three subconditions: the first that the year is divisible by 4, the second that it is not divisible by 100, and the third that it is divisible by 400.
The switch command first classifies months that have 31, then those that have 30, and finally month 2, whose number of days is the most difficult to determine. The feature of the switch command is used here, that once it enters the appropriate case, it does not interrupt the execution until it encounters the "break" command. The "break" command is therefore set only when all cases with the same number of days are listed.
The condition for a leap year includes three subconditions: the first that the year is divisible by 4, the second that it is not divisible by 100, and the third that it is divisible by 400.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int G,M,BD;
printf("Enter the year and month of the year\n");
scanf("%d%d",&G,&M);
switch(M)
{
/*months that have 31 days*/
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
BD=31;
break;
/*months that have 30 days */
case 4:
case 6:
case 9:
case 11:
BD=30;
break;
case 2:
/*If it's a leap year*/
if(((G % 4 == 0) && !(G % 100 == 0)) || (G % 400 ==0))
BD=29;
else
BD=28;
break;
}
printf("The number of days is%d\n",BD);
return 0;
}
#include <stdlib.h>
int main()
{
int G,M,BD;
printf("Enter the year and month of the year\n");
scanf("%d%d",&G,&M);
switch(M)
{
/*months that have 31 days*/
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
BD=31;
break;
/*months that have 30 days */
case 4:
case 6:
case 9:
case 11:
BD=30;
break;
case 2:
/*If it's a leap year*/
if(((G % 4 == 0) && !(G % 100 == 0)) || (G % 400 ==0))
BD=29;
else
BD=28;
break;
}
printf("The number of days is%d\n",BD);
return 0;
}