C program for printing all the months of a given particular year
Calendar Application
#include <stdio.h>
#include <conio.h>
int dayNumber(int day, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
year -= month < 3;
return (year + year / 4
- year / 100
+ year / 400
+ t[month - 1] + day)
% 7;
}
// Function that returns the name of the
// month for the given month Number
// January - 0, February - 1 and so on
char* getMonthname(int monthno.)
{
char* month; // function to return the name of the month
switch (monthno.) {
case 1:
month = "January";
break;
case 2:
month = "February";
break;
case 3:
month = "March";
break;
case 4:
month = "April";
break;
case 5:
month = "May";
break;
case 6:
month = "June";
break;
case 7:
month = "July";
break;
case 8:
month = "August";
break;
case 9:
month = "September";
break;
case 10:
month = "October";
break;
case 11:
month = "November";
break;
case 12:
month = "December";
break;
}
return month;
}
int numberOfDays(int monthNumber, int year)
{
// January
if (monthno. == 1) // for no of days in a month
return (31);
// February
if (monthno.== 2) {
// If the year is leap year then February has 29 days
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return (29);
else
return (28);
}
// March
if (monthno. == 3)
return (31);
// April
if (monthno. == 4)
return (30);
// May
if (monthno. == 5)
return (31);
// June
if (monthno. == 6)
return (30);
// July
if (monthno. == 7)
return (31);
// August
if (monthno. == 8)
return (31);
// September
if (monthno. == 9)
return (30);
// October
if (monthno. == 10)
return (31);
// November
if (monthno. == 11)
return (30);
// December
if (monthno. == 12)
return (31);
}
void printCalendar(int year)
{
printf(" Calendar :- %d\n\n", year);
int days;
int current = dayNumber(1, 1, year); //indexing calender
for (int i = 0; i < 12; i++) {
days = numberOfDays(i, year); // i is to travel through months and j for days
// Printing the month name
printf("\n *********%s*********\n",
getMonthname(i));
// Printing the week names
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
// Print the days in appropriate places
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++) {
printf("%5d", j);
if (++k > 6) {
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
int main()
{
int year = 2019;
printCalendar(year); //Calling function
return 0;
}
0 Comments