Calendar Program

Develop a program that receives the month and year from the keyboard as integers and prints the calendar in the following format
March 2006
Mon Tue Wed Thu Fri Sat Sun
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
Note that according to gregorian calendar 01/01/1900 was monday.With this as the base the calendar should be generated.

Questions by kartyy

Showing Answers 1 - 3 of 3 Answers

here is the solution:

#include<stdio.h>
#include<conio.h>
//0 is for monday, 1 for tuesday and so on
int leapyear(int year)
{
   if (year % 400 == 0)
   return 1;
   if (year % 4 == 0 && year % 100 != 0)
   return 1;
   return 0;
}
int year_days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
void main()
{
    int days, no_of_leap,yeardif,month,year,oddays,i;
    month = 1;
    clrscr();
    printf ("nEnter the month and the year: ");
    scanf ("%d%d",&month,&year);
    yeardif = year - 1900;
    no_of_leap = yeardif / 4;
    if (leapyear(year))
    no_of_leap--;
    oddays = no_of_leap * 2;
    oddays += (yeardif - no_of_leap);
 for (i=1;i<month;i++)
 {
    oddays += year_days[i];
 }
    if (leapyear(year) && month >2)
       oddays++;
    oddays = oddays % 7;
    printf ("ntMontTuetWedt  Thut   Frit   Satt     Sunn");
    for (i=0;i<oddays;i++)
       printf ("         ");
    if (leapyear(year) && month == 2)
       days = 29;
    else
       days = year_days[month];
    for (i=1;i<=days;i++)
    {
  if ((i+oddays)%7 == 1)
  printf ("n");
  printf ("%9d",i);
    }
    getch();
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions