Code:
#include <stdio.h>
#include <ctype.h>
int k,i,days;
char x1, x2;
int main(void)
{
printf("This program displays the calendar for a month.\n");
printf("Enter the number of days in the month:");
scanf("%d",&days);
printf("Enter starting day of the week (Su,Mo,Tu,...,Sa): ");
scanf(" %c %c",&x1,&x2);
x1 = toupper(x1);
x2 = tolower(x2);
printf("Su\tMo\tTu\tWe\tTh\tFr\tSa\n");
//The aligning trick starts here
if(x1=='M'&&x2=='o'){k=1;}
if(x1=='T'&&x2=='u'){k=2;}
if(x1=='W'&&x2=='e'){k=3;}
if(x1=='T'&&x2=='h'){k=4;}
if(x1=='F'&&x2=='r'){k=5;}
if(x1=='S'&&x2=='a'){k=6;}
if(x1=='S'&&x2=='u'){k=0;}
//This loop does the aligning trick
for(i=0;i<k;i++){printf("\t");}
//This asignment is neccesary for the Calender to be printed
k-=7;
//This loop prints the Calender
for(i=1; i<=days ;k++, i++)
{
if(k%7==0&&i>1)
{ //This condition ensures that a new line is printed at end of each week
printf("\n");
}
printf("%2d\t",i);
}
}