Skip to content

Instantly share code, notes, and snippets.

@dbc2201
Last active December 4, 2018 14:30
Show Gist options
  • Select an option

  • Save dbc2201/8daf7a9dffb00fb3eed995dadd9b7929 to your computer and use it in GitHub Desktop.

Select an option

Save dbc2201/8daf7a9dffb00fb3eed995dadd9b7929 to your computer and use it in GitHub Desktop.
A program in C to find out the day on a particular date.
/*
* Filename : year.c
* Author : Aryan Garg, B. Tech 1st Year, GLAU
* Modifier : Divyansh Bhardwaj, Technical Trainer, GLAU
* Aim : To find the day of the week on a given date.
* */
#include<stdio.h>
int main()
{
int year = 0, date = 0, month = 0, last_two_digits = 0, temp_m = 0, temp_l = 0,
temp_o = 0, century_code = 0, temp_p = 0, week_code = 0;
printf("Enter the date: ");
scanf("%d", &date);
printf("Enter the month (1 for Jan, 2 for Feb and so on...): ");
scanf("%d", &month);
printf("Enter the year: ");
scanf("%d", &year);
last_two_digits = year % 100; //Extracing last two digit of year
temp_m = last_two_digits + date; //add date in last two digit number of year
switch (month)
{
case 1 :
case 10 : temp_l = temp_m + 1;
break;
case 2 :
case 3 :
case 11 : temp_l = temp_m + 4;
break;
case 4 :
case 7 : temp_l = temp_m + 0;
break;
case 5 : temp_l = temp_m + 2;
break;
case 6 : temp_l = temp_m + 5;
break;
case 8 : temp_l = temp_m + 3;
break;
case 9 :
case 12 : temp_l = temp_m + 16;
break;
default: printf("Error 1"); // error 1 means switch case failed,
// value of month is invalid or zero
}
if (last_two_digits % 4 == 0) //code for checking leap year
{
if (month == 1 || month == 2) //if date is in january or
// february one is to be subtracted
{
temp_o = temp_l - 1;
}
else
{
temp_o = temp_l - 0;
}
}
if ( year >= 1700 && year < 1800 ) // add century code
{
century_code = temp_o + 4;
}
else if ( year >= 1800 && year < 1900 )
{
century_code = temp_o + 2;
}
else if ( year >= 1900 && year < 2000 )
{
century_code = temp_o + 0;
}
else if( year >= 1900 && year < 3000 )
{
century_code = temp_o + 6;
}
temp_p = century_code + last_two_digits; //add last digit of year with century_code
week_code = temp_p % 7; //week code will come
switch (week_code)
{
case 0 : printf("Sunday");
break;
case 1 : printf("Monday");
break;
case 2 : printf("Tuesday");
break;
case 3 : printf("Wednesday");
break;
case 4 : printf("Thursday");
break;
case 5 : printf("Friday");
break;
case 6 : printf("Saturday");
break;
case 7 : printf("Sunday");
break;
default: printf("Error 2"); // Error 2 means the final switch failed
// value of week_code is invalid or zero
}
return 0;
}
@dbc2201
Copy link
Author

dbc2201 commented Dec 4, 2018

The program seems to be off, it prints Wednesday on 4 12 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment