I need help finding the answer to Exercise 4, in Chapter 9 of Stephen Kochan\'s
ID: 670480 • Letter: I
Question
I need help finding the answer to Exercise 4, in Chapter 9 of Stephen Kochan's programming in C. It was recommended by my instructor to use a struct for the date to complete this exercise.
Using the following formula:
N = 1461 × f( year, month) / 4 + 153 × g( month) / 5 + day
where:
If you take the value of N and subtract 621,049 from it, and then take that result modulo 7, you get a number from 0 to 6 that represents the day of the week (Sunday through Saturday, respectively) on which the particular day falls. For example, the value of N computed for August 8, 2004, is 732,239 as derived previously. 732,239 – 621,049 gives 111,190, and 111,190 % 7 gives 2, indicating that this date falls on a Tuesday. Develop a program that displays the day of the week on which a particular date entered by the user falls. Make certain that the program displays the day of the week in English (such as “Monday”).
Explanation / Answer
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
int fnF(int year, int month) {
return month <=2 ? (year-1) : year;
}
int fnG(int month) {
return month <=2 ? (month+1) : month;
}
void printDay(int N) {
int day = N % 7;
if(day == 0)
printf("Sunday");
if(day == 1)
printf("Monday");
if(day == 2)
printf("Tueday");
if(day == 3)
printf("Wedday");
if(day == 4)
printf("Thuday");
if(day == 5)
printf("Friday");
if(day == 6)
printf("Satday");
}
int calculateN(struct Date date) {
int num;
int fnFVal = fnF(date.year, date.month);
int fnGval = fnG(date.month);
num = (1461 * fnFVal) / 4 + (153 * fnGval) / 5 + date.day;
return num;
}
int main()
{
struct Date date;
int day, month, year;
printf("Enter Day :");
scanf("%d",&day);
printf("Enter Month :");
scanf("%d",&month);
printf("Enter Year :");
scanf("%d",&year);
date.day = day;
date.month = month;
date.year = year;
int N = calculateN(date);
printDay(N);
return 0;
}#include <stdio.h>
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
int fnF(int year, int month) {
return month <=2 ? (year-1) : year;
}
int fnG(int month) {
return month <=2 ? (month+1) : month;
}
void printDay(int N) {
int day = N % 7;
if(day == 0)
printf("Sunday");
if(day == 1)
printf("Monday");
if(day == 2)
printf("Tueday");
if(day == 3)
printf("Wedday");
if(day == 4)
printf("Thuday");
if(day == 5)
printf("Friday");
if(day == 6)
printf("Satday");
printf(" ");
}
int calculateN(struct Date date) {
int num;
int fnFVal = fnF(date.year, date.month);
int fnGval = fnG(date.month);
num = (1461 * fnFVal) / 4 + (153 * fnGval) / 5 + date.day;
return num;
}
int main()
{
struct Date date;
int day, month, year;
printf("Enter Day : ");
scanf("%d",&day);
printf("Enter Month : ");
scanf("%d",&month);
printf("Enter Year : ");
scanf("%d",&year);
date.day = day;
date.month = month;
date.year = year;
int N = calculateN(date);
printDay(N);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.