Programming Examples
Write a program to display days in a calendar
Write a program to display days in a calendar format of an entered month of year 2015.
Solution
#include<stdio.h>
#include<conio.h>
void main()
{
int m,h,i=1,a,j,b=1;
clrscr();
printf (“\n Enter Month Number of the Year 2015 : “);
scanf (“%d”,&m); /* Program for finding days of any month of 2015*/
switch(m)
{
case 1 :
a=5;
j=31;
break;
case 2 :
a=1;
j=28;
break;
case 3 :
a=1;
j=31;
break;
case 4 :
a=4;
j=30;
break;
case 5 :
a=6;
j=31;
break;
case 6 :
a=2;
j=30;
break;
case 7 :
a=4;
j=31;
break;
case 8 :
a=7;
j=31;
break;
case 9 :
a=3;
j=30;
break;
case 10 :
a=5;
j=31;
break;
case 11 :
a=1;
j=30;
break;
case 12 :
a=3;
j=31;
break;
default :
printf (“\a\a Invalid Month”);
exit();
}
/* starting day is to be shown/adjusted as per calendar */
printf (“\n\n\n”);
printf (“\t\t\tMonth - %d - 2015\n\n”,m);
printf (“ SUN MON TUE WED THU FRI 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; /* The starting day is subtracted from 8 */
for (i=2;i<=h;i++) /* To display the first row */
printf (“\t%d”,i);
printf (“\n”);
for (i=h+1; i<=j;i++) /* To continue with second row onwards */
{
if (b==8) /* To terminate the line after every week */
{
printf (“\n”);
b=1;
}
printf (“\t%d”,i);
b++;
}
getch();
}
Output
OUTPUT:
Enter Month Number of the Year 2015: 2
Month – 2 – 2015
SUN MON TUE WED THU 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