Hello, I'm new to C and I need to make a program wherein the user inputs a number (1-12 corresponding to each month) and a year ( from 1978-3000).
The output should be the calendar for the specified date and year.
There should be 7 columns, one for each day. I should also take leap years into consideration which happens every 4 years, but not every 100 years, but happens again every 400 years.
I have this so far.
Code:
#include <stdio.h>
#include <string.h>
char month_name[12][9] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int day_count(int month, int year)
{
int day = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
day = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11){
day = 30;
} else if (month == 2){
if (year%4 == 0 && year%100 != 0 && year%400 == 0){
day = 29;
} else {
day = 28;
}
}
return day;
}
int first_day(int year)
{
int init_pos;
int x;
int y;
int z;
x = (year - 1)/4.0;
y = (year - 1)/100.0;
z = (year - 1)/400.0;
init_pos = (year + x - y + z) % 7;
return init_pos;
}
int main(void)
{
int month;
int year;
int i;
do{
printf("Enter month (1 - 12): ");
scanf("%d", &month);
} while (month < 1 || month > 12);
do{
printf("Enter year (1978 - 3000): ");
scanf("%d", &year);
} while (year < 1978 || year > 3000);
//printf("%s", month_name[month-1]);
printf("\n %d Calendar \n", year);
printf("%s\n", month_name[month-1]);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
return 0;
}
I can't think of how I could print out the numbers under the name of the days.
I'm also not sure if my formula for my first_day function will give the right day for each month of the year.
I really need help.
Thanks!