Sei sulla pagina 1di 3

DS Lab

Date: 3 Feb 2015

Q. Write a c program to print the day for a given date.


Logic: This program prints the day of a given date.
Example:
input: day=22,month=1,year=2013
output: Wednesday
In the program, use the concept of odd days
Odd days: In a given period, the number of days more than the complete weeks are called odd days
Leap year:

A year which is divisible by 4 is a leap year with exception for century


Every 4th century is a leap year and no other century is a leap year
A leap year has no. of days=366 days where as ordinary years have 365 days.

Example:
1. years 1804,1908,2012,2016 are leap years.
2. years 400,800,1200,1600 are leap years.
3. years 1999,2001,2002,2003,1800,600 are not leap years.
Q. How to count odd days?
(I) 1 ordinary year = 365 days = (52 weeks + 1 day). So one ordinary year has 1 odd day.
(II) 1 leap year=366 days=(52 weeks + 2days). So one leap year has 2 odd days.
(III)100 years has 76 ordinary years + 24 leap years = (761+242)odd days = 124 odd days
=(17 weeks + 5 days) = 5 odd days
So,
Number of odd days in 100years = 5 odd days
Number of odd days in 200years = 52 = 3 odd days ( and 1 week)
Number of odd days in 300years = 53 = 1 odd days ( and 2 weeks)
Number of odd days in 400years = 54 + 1 (extra 1 for leap year) = 0 odd days
so, each of years 800,1200,1600, etc has 0 odd days.
How odd days are related to day of week?
ODD DAYS

DAY

SUNDAY MONDAY TUESDAY WEDNESDAY

THURSDAY FRIDAY SATDAY

Example
Given date 25th Feb 2013
so, odd days = ((odd days till 2013) + (days 1.1.2013 to 25.2.2013))%7
using the above table we can find out which day it is for the corresponding odd-days value

CODE
#include<stdio.h>
#define IS_LEAP_YEAR(Y) (((Y%4 == 0)&&(Y%100 != 0))||(Y% 400 == 0))
#define ODD_DAYS_LEAP_YEAR 2
#define ODD_DAYS_ORDINARY_YEAR 1
int validateInput(int,int,int);
char dayStrings[7][10]={"Sunday","Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday"};
int daysInMonth[12]={31,28,31,30,31, 30,31,31,30,31,30,31};
/*it stores the odd days for 100,200,300 years odd days
*i.e 5,3,1 odd days resp. */
int yearMap[4]={0,5,3,1};
int main()
{
int day,month,year,OrdinaryYears,TOddDays,i,Tdays=0;
int CYears,OddYears,OddDays=0,LeftYears,leapYears;
printf("Enter the Day(1-31):");
scanf("%u",&day);
printf("\nEnter the Month(1-12):");
scanf("%u",&month);
printf("\nEnter the Year:");
scanf("%u",&year);
printf("\n\nInputDate : %02u/%02u/%04u",day,month,year);
/*validate the Inputs*/
if(0 == validateInput(day,month,year))
return 1;
/*Get the completed years*/
CYears = year-1;
/*getting the no of years after the century with 0 odd days*/
OddYears = CYears%400;
/*get the Odd days for complete centuries out of the Oddyears found above.
*i.e 100 years = 5 odd days ,200 years = 3 odd days,etc. */
OddDays += yearMap[OddYears/100];
/*get the years left out (<100)*/
LeftYears = OddYears%100;
/*calculate leap years*/
leapYears = LeftYears/4;
/*calculate ordinary years*/
OrdinaryYears = LeftYears - leapYears;
/*total odd days for left out years*/

TOddDays = leapYears*ODD_DAYS_LEAP_YEAR + OrdinaryYears *


ODD_DAYS_ORDINARY_YEAR;
/*Getting overall odd days after adding with odd days of left out years*/
OddDays += TOddDays%7;
for(i=0;i<month-1;i++)
{
Tdays += daysInMonth[i];
}
Tdays += day;
OddDays = (OddDays + Tdays)%7;
printf("\nThe day is %s\n",dayStrings[OddDays]);
return(0);
}/*end of main()*/
/*validates date and also changes the feb month days to 29 in daysInMonth array if it is a leap year */
int validateInput(int day,int month,int year)
{
//if month is geater than 12
if(month>12)
{
printf("\n\nIncorrect Month");
return 0;
}
//if given month is february then check for leap year
//if leap year then check for no of day
//if no of days is greater than 29 print the error message
if(2 == month)
{
if(IS_LEAP_YEAR(year))
{
if(day>29)
{
printf("\n\nIncorrect no of days for the given Month");
return 0;
}
/*increment the feb month days*/
daysInMonth[1]+=1;
return 1;
}
}
//check for the valid no of days for a given month
if(day>daysInMonth[month-1])
{
printf("\n\nIncorrect no of days for the given Month");
return 0;
}
return 1;
}

Potrebbero piacerti anche