Write a program that will ask the user to input the day, month and year in numerical form, format: dd mm yyyy. Then, display the day of the week and the day of the year of the date entered by the user. Your program should continue this process until zero is given for value of the day, month and year. For instance:
Enter date: 05 12 2008
Will produce;
May 12, 2008 is Monday and that is the 133 day of the year.
Note: The last day of 2000 (31/12/2000) was a Sunday. This was the 366th day of the year. The year 2000 was a leap year.
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
This will work for day of week.
#include <stdio.h>
#define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
int isdatevalid(int month, int day, int year);
int weekday(int month, int day, int year);
char week[7][10] = {
"Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"
};
int main()
{
int month, day, year;
printf("Return the day of the week given the date.");
printf("Enter date in the form mm/dd/yyyy : ");
scanf("%d/%d/%d",&month,&day,&year);
if (isdatevalid(month,day,year))
{
printf("This date is a %s",
week[weekday(month,day,year)]);
}
else
printf("%d/%d/%d not a valid date!",
month,day,year);
getchar(); // wait
getchar(); // 2nd wait needed
return 0;
}
int isdatevalid(int month, int day, int year)
{
if (day <= 0) return 0 ;
switch( month )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : if (day > 31) return 0 ; else return 1 ;
case 4 :
case 6 :
case 9 :
case 11 : if (day > 30) return 0 ; else return 1 ;
case 2 :
if ( day > 29 ) return 0 ;
if ( day < 29 ) return 1 ;
if (isleapyear(year)) return 1 ; // leap year
else return 0 ;
}
return 0 ;
}
// given month, day, year, returns day of week, eg. Monday = 0 etc.
int weekday(int month, int day, int year)
{
int ix, tx, vx;
switch (month) {
case 2 :
case 6 : vx = 0; break;
case 8 : vx = 4; break;
case 10 : vx = 8; break;
case 9 :
case 12 : vx = 12; break;
case 3 :
case 11 : vx = 16; break;
case 1 :
case 5 : vx = 20; break;
case 4 :
case 7 : vx = 24; break;
}
if (year > 1900) // 1900 was not a leap year
year -= 1900;
ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
return (tx % 7);
}
how could we solve this.
we dont even know what programming language do you use?
i think you forgot on what platform..