Visualizing Calendar Days for the Year 2001 in C Programming
In this C programming exploration, we dive into a code snippet designed to display the number of days in a calendar format for a specified month of the year 2001. The program takes user input for the month, calculates the days, and organizes them in a calendar layout. Let's break down the code to understand its functionality.
The Code:
#include<stdio.h>
main()
{
int m, h, i = 1, a, j, b = 1;
printf("\n Enter the month no. of year 2001 : ");
scanf("%d", &m);
switch(m)
{
case 1:
a = 2;
j = 31;
break;
case 2:
a = 5;
j = 28;
break;
case 3:
a = 5;
j = 31;
break;
case 4:
a = 1;
j = 30;
break;
case 5:
a = 3;
j = 30;
break;
case 6:
a = 6;
j = 30;
break;
case 7:
a = 1;
j = 31;
break;
case 8:
a = 4;
j = 31;
break;
case 9:
a = 7;
j = 30;
break;
case 10:
a = 2;
j = 31;
break;
case 11:
a = 5;
j = 30;
break;
case 12:
a = 7;
j = 31;
break;
default:
printf("\a\a Invalid Month ");
break;
}
printf("\n\n");
printf("\t\t Month %d of 2001\n\n", m);
printf("\t SUN \t MON \t TUE \t WED \t THR \t FRI \t SAT \n\n");
switch(a)
{
case 1:
printf("\t %d", i);
break;
case 2:
printf("\t \t %d", i);
break;
case 3:
printf("\t \t \t %d", i);
break;
case 4:
printf("\t \t \t \t %d", i);
break;
case 5:
printf("\t \t \t \t \t %d", i);
break;
case 6:
printf("\t \t \t \t \t \t %d", i);
break;
case 7:
printf("\t \t \t \t \t \t \t %d", i);
break;
}
h = 8 - a;
for(i = 2; i < = h; i++)
printf("\t %d ", i);
printf("\n");
for(i = h + 1 ; i < = j; i++)
{
if(b == 8)
{
printf("\n");
b = 1;
}
printf("\t %d", i);
b++;
}
}
Explanation:
User Input:
The program prompts the user to enter the month number of the year 2001.
Switch Statement:
The code employs a switch statement to determine the number of days and the starting day of the week for the specified month.
Calendar Display:
After determining the necessary details, the program prints a calendar for the specified month, organized by days of the week.
Input and Output Example:
Assuming the user enters 1 for January:
- Input : Enter the month of year 2001 : 1
- Output :
Month 1 of 2001
SUN MON TUE WED THR FRI SAT
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
0 Comments