Hi, this is the C code which I wrote to print one month’s calendar. This program takes care of the following situations or failure cases:
- It prints the dates as per the usual calendar layout
- It takes care of the situation where the month doesn’t start on Sunday.
- It takes care of the situation where the user may enter the number of days less than 28 or more than 31.
- It takes care of the situation where the user may enter the “start day” number less than 1 or more than 7.
/*
C Program to print one month's calender in proper format
Programmed By: www.123mylist.com
*/
#include <stdio.h>
#include <conio.h>
int main()
{
int i,days,start;
do{
printf("Enter the number of days\n");
scanf("%d",&days);
}while(days<28 | days>31);
do{
printf("\nEnter the start day number 1-Sun; 2-Mon; ... ; 7-SAT");
scanf("%d",&start);
}while(start<0 | start>7);
printf("\nSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\n");
for(i=1;i<=days+start-1;i++)
{
if(i<start)
{
printf(" \t");
}
else
{
printf("%d\t",i-start+1);
}
if((i%7)==0)
printf("\n");
}
getch();
return 0;
}