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