You are on page 1of 9

#include<stdio.

h>

#include<conio.h>

int first_day(int, int, int);


int no_of_days(int, int, int * );
int random_date();

void main() {
int n1, n2, yr, i, j, date;
int month;
int leap_year = 0;
a:
printf("Enter the month no:\t");
scanf("%d", & month);
printf("Enter the year :\t");
scanf("%d", & yr);
char c;
d:
n2 = no_of_days(month, yr, & leap_year); //checks if the year is leap or not
and changes the value of leap year variable acentury_codeordingly, and also returns
no of days of month
if (n2 == 0) // when user enters an invalid month number
goto a;
n1 = first_day(month, yr, leap_year); //returns the day code for first of the
month i.e 0 for sunday, 1 for monday,etc.
char * months[12] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
//printing the calendar

for (i = 0; i < 12; i++) {


if ((month - 1) == i) {
printf("\n\t\t%s\t%d\n", months[i], yr);
break;
}
}
date = 1;
printf("SUN\tMON\tTUE\tWED\tTHURS\tFRI\tSAT\n");

for (i = 0; date <= n2; i++) {


if (date == 1) {
if ((leap_year == 1) && (month > 2)) {
for (j = 0; j < n1 + 1; j++) {
printf("\t");
}
for (j = n1 + 1; j <= 6; j++) {
printf("%d\t", date);
date++;
}
printf("\n");
} else {
for (j = 0; j < n1; j++) {
printf("\t");
}
for (j = n1; j <= 6; j++) {
printf("%d\t", date);
date++;
}
printf("\n");
}
}

for (j = 0; j <= 6; j++) {


printf("%d\t", date);
date++;
if (date > n2)
break;
}

printf("\n");

switch (month) {
case 1:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n1-New Year\n");
printf("14-Makarsankrant\n");
printf("26-Republic day\n");
goto skip;
case 2:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n19-Chhatrapati Shivaji Maharaj Jayanti");
goto skip;
case 3:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n---\n");
goto skip;
case 4:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n14-Dr. Ambedkar Jayanti\n");
printf("\n17-Mahavir Jayanti\n");
goto skip;
case 5:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n1-Maharashtra Day\n");
goto skip;
case 6:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n---\n");
goto skip;
case 7:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n---\n");
goto skip;
case 8:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n15-Independence Day\n");
goto skip;
case 9:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n---\n");
goto skip;
case 10:
printf("\n2-Gandhi Jayanti\n");
printf("\n31-Sardar Vallabhbhai Patel Jayanti\n");
goto skip;
case 11:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("14-Children's day\n");
goto skip;
case 12:
printf("\n\nPUBLIC HOLIDAYS IN %s", months[month - 1]);
printf("\n25-Christmas\n");
goto skip;
}
skip:

jump5:
printf("\nHit 'Y' if you want to continue else 'N' to exit the program : ");
jump:
fflush(stdin);
scanf(" %c", & c);

if (c == 'n' || c == 'N') {
goto b;
} else if (c == 'y' || c == 'Y') {
clrscr();
printf("\n");
printf("1. Do you want to look at the consecutive months or years? ");
printf("\n");
printf("2. Or do you want to just have a peek at certain month of some year?
");
printf("\n");
printf("3. Do you want to find out which day was on which day? ");
printf("\n");
printf("Press 1, 2, 3 accordingly. ");
char next_check;
jump2:
scanf(" %c", & next_check);
if (next_check == '1') {
jump3: clrscr();
printf("Enter 'w' to print calendar for %s %d\n", months[month - 1], yr - 1);
printf("Enter 's' to print calendar for %s %d\n", months[month - 1], yr + 1);
if (month == 1) {
printf("Enter 'a' to print calendar for %s %d\n", months[11], yr - 1);
} else {
printf("Enter 'a' to print calendar for %s %d\n", months[month - 2], yr);
}
printf("Enter 'd' to print calendar for %s %d\n", months[month], yr);
fflush(stdin);
scanf(" %c", & c);
clrscr();
if (c == 'w') {
yr = yr - 1;
goto d;
}
if (c == 's') {
yr = yr + 1;
goto d;
}
if (c == 'a') {
if (month == 1) {
month = 12;
yr = yr - 1;
goto d;
}
month = month - 1;
goto d;
}
if (c == 'd') {
month = month + 1;
goto d;
} else {
jump4: printf("\nInvalid input!! Do you want to still continue? ");
char aaa1;
scanf(" %c", & aaa1);
if (aaa1 == 'y' || aaa1 == 'Y') {
goto jump3;
} else if (aaa1 == 'n' || aaa1 == 'N') {
goto b;
} else {
goto jump4;
}
}
}
else if (next_check == '2') {
clrscr();
goto a;
} else if (next_check == '3') {
clrscr();
date = random_date();

if (date == 1) {
printf("\t MONDAY");
}
if (date == 2) {
printf("\t TUESDAY");
}
if (date == 3) {
printf("\t WEDNESDAY");
}
if (date == 4) {
printf("\t THURSDAY");
}
if (date == 5) {
printf("\t FRIDAY");
}
if (date == 6) {
printf("\t SATURDAY");
}
if (date == 0) {
printf("\t SUNDAY");
}
goto jump5;

} else {
clrscr();
printf("Invalid input!! \n");
goto jump2;
}

} else {
printf("\nInvalid input!! Do you still want to continue? ");
goto jump;
}

b: printf(" ");

int no_of_days(int month, int yr, int * p) {


//checking leap year
if (yr % 4 == 0) {
if (yr % 100 == 0) {
if (yr % 400 == 0) {
* p = 1;
} else {
* p = 0;
}
} else {
* p = 1;
}
} else {
* p = 0;
}

//determining no of days of the month


switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
return (31);
break;
}

case 2:
{
if ( * p == 0) {
return (28);
} else if ( * p == 1) {
return (29);
}
break;
}

case 4:
case 6:
case 9:
case 11:
{
return (30);
break;
}
default:
{
printf("Invalid month input!! ");
return 0;
}

}
}

int first_day(int month, int yr, int leap_year) {


//calculating year code
int year_code, yy;
yy = yr % 100;
year_code = (yy + (yy / 4)) % 7;
// calculating month code
int month_code;
switch (month) {
case 1:
{
month_code = 0;
break;
}
case 3:
{
month_code = 3;
break;
}
case 5:
{
month_code = 1;
break;
}
case 7:
{
month_code = 6;
break;
}
case 8:
{
month_code = 2;
break;
}
case 10:
{
month_code = 0;
break;
}
case 12:
{
month_code = 5;
break;
}
case 2:
{
month_code = 3;
break;
}

case 4:
{
month_code = 6;
break;
}
case 6:
{
month_code = 4;
break;
}
case 9:
{
month_code = 5;
break;
}
case 11:
{
month_code = 3;
break;
}
default:
printf("Invalid month input! ");

}
//calculating century code
int century_code;
if ((yr - yy - 1700) % 400 == 0)
century_code = 4;
else if ((yr - yy - 1800) % 400 == 0)
century_code = 2;
else if ((yr - yy - 1900) % 400 == 0)
century_code = 0;
else if ((yr - yy - 2000) % 400 == 0)
century_code = 6;
//returning the day code using Zellner's formula
return ((year_code + month_code + century_code + 1 - leap_year) % 7);
}
int random_date() {
printf("\nEnter Date , month , year one by one :\n");
int da, mn, ye1;
scanf("%d%d%d", & da, & mn, & ye1);
int leap;
//checking leap year
if (ye1 % 4 == 0) {
if (ye1 % 100 == 0) {
if (ye1 % 400 == 0) {
leap = 1;
} else {
leap = 0;
}
} else {
leap = 1;
}
} else {
leap = 0;
}
//calculating year code
int year_code, yy;
yy = ye1 % 100;
year_code = (yy + (yy / 4)) % 7;
// calculating month code
int month_code;
switch (mn) {
case 1:
{
month_code = 0;
break;
}
case 3:
{
month_code = 3;
break;
}
case 5:
{
month_code = 1;
break;
}
case 7:
{
month_code = 6;
break;
}
case 8:
{
month_code = 2;
break;
}
case 10:
{
month_code = 0;
break;
}
case 12:
{
month_code = 5;
break;
}

case 2:
{
month_code = 3;
break;
}

case 4:
{
month_code = 6;
break;
}
case 6:
{
month_code = 4;
break;
}
case 9:
{
month_code = 5;
break;
}
case 11:
{
month_code = 3;
break;
}
default:
printf("Invalid month input! ");

}
//calculating century code
int century_code;
if ((ye1 - yy - 1700) % 400 == 0)
century_code = 4;
else if ((ye1 - yy - 1800) % 400 == 0)
century_code = 2;
else if ((ye1 - yy - 1900) % 400 == 0)
century_code = 0;
else if ((ye1 - yy - 2000) % 400 == 0)
century_code = 6;
//returning the day code using Zellner's formula
return ((year_code + month_code + century_code + da - leap) % 7);