Solution code:-
=========
Easy One (Irrespective of difference between year input from year 1901) :
/*According to the Gregorian calender, it was Monday on the date 01/01/01. If any year is input
* through the keyboard write a program to find out what is the day on 1st January of this year?*/
#include <stdio.h>
int main(void)
{
int yr,diff,lpyrdays,normaldays,res;
printf("\nEnter a year whose day of 1st Jan you want to know : ");
scanf("%d",&yr);
yr = (yr - 1) ; //removing 1 year as only 1 day we are calculating of current yr
lpyrdays = (yr/4) + (yr / 400) - (yr / 100 ); //Calculating leap days in that particular year
normaldays = (yr* 365 )+ 1 + lpyrdays ; //Calculating normal days in that year & adding 1st jan's 1 day in
res = normaldays % 7;
if(res==0)
printf("\nSunday");
if(res==1)
printf("Monday");
if(res==2)
printf("Tuesday");
if(res==3)
printf("Wednesday");
if(res==4)
printf("Thursday");
if(res==5)
printf("Friday");
if(res==6)
printf("Saturday");
return 0;
}
Tough One (Solution Code):
#include <stdio.h>
int main(void) //main function.. starting of c code
{
int year,differ,lp_year,day_type;
long int days;
printf("Please enter the year: ");
scanf("%d",&year);
year=year-1; //we will find days before given year so
differ=year-1900;
/*as leap year is not divisible by 100.so,create 2 condition
one difference less than 100 and greater than 100*/
if(differ<100)
{
lp_year=differ/4; //caln of total no. of leap year
days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note1
day_type=days%7; //caln of day type sun, mon......
}
if(differ>=100)
{
lp_year=(differ/4)-(differ/100)+1+((year-2000)/400);//see Note2
days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note3
day_type=days%7;
}
if(day_type==0)
printf("\nSunday");
if(day_type==1)
printf("Monday");
if(day_type==2)
printf("Tuesday");
if(day_type==3)
printf("Wednesday");
if(day_type==4)
printf("Thursday");
if(day_type==5)
printf("Friday");
if(day_type==6)
printf("Saturday");
return 0; //int main() is function so value must be return.
//u will read in function chapter
}
/*Note1:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type
Note2:
-(leap year come in every 4 year so) (differ/4) for leap year
-(leap year isn't divisible by 100 so we subtract (differ/100)
from counting as leap year
-(leap year will be if divisible by 400 so ((year-2000)/400)
to count that year as leap year
- we calculate from 2000 so we add 1
Note3:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type */