I'm trying to make a calendar out of C. I need it to be able to promt the user for a month, and the year, and it should then print the calendar of that.

So far the code I've got is:

#include<stdio.h>

int days_in_month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
  {
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
  };

int determinedaycode(int year)
{
  int daycode;
  int d1, d2, d3;
  d1 = (year - 1.)/4.0;
  d2 = (year - 1.)/100.;
  d3 = (year - 1.)/400.;
  daycode = (year + d1 +d2 +d3) %7;
  return daycode;
}

int determineleapyear(int year) 
{ 
   //leap function 1 for leap & 0 for non-leap
   if ((year % 100 == 0) && (year % 400 != 0))
      return 0;
   else if (year % 4 == 0)
      return 1;
   else
      return 0;
}

void calendar(int year, int daycode)
{
  int month, day;
  for (month = 1; month <= 12; month++)
    {
      printf("%s", months[month]);
      printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n");

      //Correct the position for the first date
      for (day = 1; day <= 1 + daycode * 5; day++)
    {
      printf(" ");
    }

      //Print all the dates for one month
      for (day = 1; day <= days_in_month[month]; day++)
    {
      printf("%2d", day);

      //Is day before Sat? Else start next line Sun.
      if ((day + daycode) % 7 > 0)
        printf("   ");
      else
        printf("\n ");
    }
      // Set position for next month
      daycode = (daycode + days_in_month[month]) % 7;
    }
}

//------------------------------------------
int main() {
  int daycode, month, year;

   printf("\nEnter the month: ");
   scanf("%d", &month);

   printf("\nEnter the year: ");
   scanf("%d", &year);

   daycode = determinedaycode(year);
   calendar(year, daycode);
   printf("\n");

   return 0;
}

My issue is that it prints the calendar for the entire year, and plus the dates are off by two days. Any help would be great

I tried this code:

int determinedaycode(int year)
            {
              int d1, d2, d3;
              d1 = year / 4;
              d2 = year / 100;
              d3 = year / 400;
              return 1 + (year + d1 + d2 + d3 + 1) % 7;
            }

Seems to work, I only cannot garantee this will work all the time. :)
Happy computing.
BTW: you are not using your leapyear function.

Edited 2 Years Ago by ddanbe: addition

Awesome, that fixed the issue where the calendar days were not matching up.

How do I incorporate the leapyear function? I've tried adding it, but it didn't work. And I still need help with how to be able to print just the month calendar of which the user asks for, rather than the entire year.

#include<stdio.h>

int days_in_month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
  {
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
  };

int determinedaycode(int year)
{
  int d1, d2, d3;
  d1 = year/4.0;
  d2 = year/100.;
  d3 = year/400.;
  return 1 + (year + d1 + d2 + d3 + 1) % 7;
}

int determineleapyear(int year) 
{ 
   //leap function 1 for leap & 0 for non-leap
   if ((year % 100 == 0) && (year % 400 != 0))
      return 0;
   else if (year % 4 == 0)
      return 1;
   else
      return 0;
}

void calendar(int year, int daycode)
{
  int month, day;
  for (month = 1; month <= 12; month++)
    {
      printf("%s", months[month]);
      printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n");

      //Correct the position for the first date
      for (day = 1; day <= 1 + daycode * 5; day++)
    {
      printf(" ");
    }

      //Print all the dates for one month
      for (day = 1; day <= days_in_month[month]; day++)
    {
      printf("%2d", day);

      //Is day before Sat? Else start next line Sun.
      if ((day + daycode) % 7 > 0)
        printf("   ");
      else
        printf("\n ");
    }
      // Set position for next month
      daycode = (daycode + days_in_month[month]) % 7;
    }
}

//------------------------------------------
int main() {
  int daycode, month, year, leapyear;

   printf("\nEnter the month: ");
   scanf("%d", &month);

   printf("\nEnter the year: ");
   scanf("%d", &year);

   daycode = determinedaycode(year);
   calendar(year, daycode);
   leapyear = determineleapyear(year);
   printf("\n");

   return 0;
}

For incorporating the leapyear, I would test if your month is 2(februari) then, instead of just using month, use month + determineleapyear(year)
For printing just one month, improve your code by first adding a function
called say print_a_month(int amonth).

You have the code, get it out of the for loop in calendar function.
Add a bool parameter to your calendar function to decide if you want to print one month or a whole year.
Success!

Edited 2 Years Ago by ddanbe: correction

Decided to just convert the calendar function to the print_a_month function. I'm not sure where to put the amonth function part though.
And where should I incorporate the leapyear month code?

#include<stdio.h>

int days_in_month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
  {
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
  };

int determinedaycode(int year)
{
  int d1, d2, d3;
  d1 = year / 4.0;
  d2 = year / 100.;
  d3 = year / 400.;
  return 1 + (year + d1 + d2 + d3 + 1) % 7;
}

int determineleapyear(int year) 
{ 
   //leap function 1 for leap & 0 for non-leap
   if ((year % 100 == 0) && (year % 400 != 0))
      return 0;
   else if (year % 4 == 0)
      return 1;
   else
      return 0;
}

int print_a_month(int year, int daycode, int amonth)
{
  int month, day;
  for (month = 1; month <= 12; month++)
    {
      printf("%s", months[month]);
      printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n");

      //Correct the position for the first date
      for (day = 1; day <= 1 + daycode * 5; day++)
    {
      printf(" ");
    }

      //Print all the dates for one month
      for (day = 1; day <= days_in_month[month]; day++)
    {
      printf("%2d", day);

      //Is day before Sat? Else start next line Sun.
      if ((day + daycode) % 7 > 0)
        printf("   ");
      else
        printf("\n ");
    }
      amonth = printf("%d", days_in_month[month]);
    }
}

//------------------------------------------
int main() {
  int daycode, month, year, leapyear;

   printf("\nEnter the month: ");
   scanf("%d", &month);

   printf("\nEnter the year: ");
   scanf("%d", &year);

   daycode = determinedaycode(year);
   print_a_month(amonth);
   leapyear = determineleapyear(year);
   printf("\n");

   return 0;
}

OK, but get line 44(the for loop out of print_a_month.
Now in your main function ask the user if he wants to print one month(then ask which)
print the month or else print a whole year in you for loop.
The leapyear jumps in when you test(in this loop if the month == 2 you use days_in_month[month]+leapyear(month) instead of just days_in_month[month]
Guess I confused you a bit in my previous post.

Pretty sure I just got even more confused. I'm just trying to have the program ask the user for a month, then a year, and to print the month of that year. I don't need to print out the entire year.

Am I incorporating the leapyear in the print_a_month function or the main function?

In print_a_month do something like this:

int dayspermonth;
            dayspermonth = days_in_month[month];
            if (month == 2) dayspermonth = dayspermonth + determineleapyear(year);

Remove in the for loop on line 56 days_in_month[month]
change it in dayspermonth
Hope it clears things out.
See ya tomorrow perhaps. :)

Found out there was more wrong in your determinedaycode function.
Here is what I have now:

int determinedaycode(int year, int month, int day)
        {
            int d1, d2, d3, d4, code;
            if (month == 1)
            {
                month = 13; year--;
            }
            if (month == 2)
            {
                month = 14; year--;
            }
            d1 = year / 4;
            d2 = year / 100;
            d3 = year / 400;
            d4 = day + 2 * month + 3 * (month + 1) / 5 + year;
            code = (d4 + d1 - d2 + d3 + 2) % 7;
            if (code == 0) code = 7;
            return code;
            // code will be sun=1,mon=2,tue=3...sat=7
        }

This is a result I obtained:

Attachments Kalender.png 36.12 KB
This article has been dead for over six months. Start a new discussion instead.