I am trying to write a simple program, that outputs the day given a date, using 1/1/0001 as Saturday. I have written the program, but have passed about 4 hours just to find some mistake which is making my answer wrong. The correct answer can be found here. http://www.timeanddate.com/date/durationresult.html?d1=1&m1=1&y1=1&d2=1&m2=1&y2=400&ti=on
The following is my program:
#include <stdio.h>
#include <string.h>
long long int leap_days, days;
long long int *no_leap_years(int day, int month, long long int year){
leap_days = (year-1)/4;
leap_days -= (year-1)/100;
leap_days += (year-1)/400;
int is_leap;
if(!(year%400)){
is_leap = 1;
} else if(!(year%100)){
is_leap = 0;
} else if(!(year%4)){
is_leap = 1;
} else {
is_leap = 0;
}
printf("Leap year - %d\n", is_leap);
printf("Leap days - %lld\n", leap_days);
if(is_leap){
if(month>2) leap_days++;
else if(month==2){
if(day>28) leap_days++;
}
}
printf("Leap days - %lld\n", leap_days);
return &leap_days;
}
long long int *no_of_days(int day, int month, long long int year){
long long int leap_days = *no_leap_years(day, month, year);
days = leap_days + (365*(year-1));
if(month>1) days+=31;
if(month>2) days+=28;
if(month>3) days+=31;
if(month>4) days+=30;
if(month>5) days+=31;
if(month>6) days+=30;
if(month>7) days+=31;
if(month>8) days+=31;
if(month>9) days+=30;
if(month>10) days+=31;
if(month>11) days+=30;
days+=day;
printf("total - %lld\n", days);
return &days;
}
char *day_computer(int day, int month, long long int year){
long long int days = *no_of_days(day, month, year);
printf("total - %lld\n", days);
days %= 7;
printf("remain - %lld\n", days);
if(days==1) return "Saturday";
else if(days==2) return "Sunday";
else if(days==3) return "Monday";
else if(days==4) return "Tuesday";
else if(days==5) return "Wednesday";
else if(days==6) return "Thursday";
else return "Friday";
}
int main()
{
int dd = 1;
int mm = 1;
long long int yy = 400;
printf("%s\n", day_computer(dd, mm, yy));
return 0;
}
According to the website, total no.of days from 1/1/1 to 1/1/400 should be 145,735 days, but according to my code, it is 145732. I cannot find where I am missing 3 days. Please help me.
You may use this online editor to compile my program. http://www.compileonline.com/compile_c_online.php
gcc -Wall -gif using GCC...). Learn how to use the debugger (e.g.gdbon Linux).