Answer to Question #269391 in C for tony

Question #269391

write a program that will find the day of the week from a date and will also prepare a calendar on a monthly basis. (Science: day of the week). July 20, 1973 is a FRIDAY using Zeller’s congruence is an algorithm developed by Christian Zeller

h=(q+[26(m+1)/10] + y +[y/4] + 6 * [y/100]+[y/400]) % 7

Where,

■ h is the day of the week (0: Saturday, 1: Sunday, 2: Monday.....)

■ q is the day of the month

■ m is the month (3: March, 4: April, ….., 12: December)

■ y is the year

NOTE: In this algorithm, January and February are counted as months 13 and 14 of the previous year. E.g. if it is February 2, 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format). Consequently, for January and February, the year y should be replaced by y – 1. Suffice to say, the month m should also be replaced by m + 12 then.


Sample Input/Output :


Input: Enter year: 2005 Enter month (1-12): 1 Enter day: 21

Output: January 21, 2005 is FRIDAY


1
Expert's answer
2021-11-21T04:49:19-0500
#include<stdio.h>
 
int dayofweek(int d, int m, int y)
{
    static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
 
int main()
{
	int d,m,y;
	printf("Enter the day of the week: ");
	scanf("%d",&d);
	printf("Enter month: ");
	scanf("%d",&m);
	printf("Enter the year: ");
	scanf("%d",&y);
    int day = dayofweek(d, m, y);
    printf ("%d", day);
    if(day==0){
    	printf(" is a Saturday");
	}
	else if(day==1){
		printf(" is a Sunday");
	}
	else if(day==2){
		printf(" is a Monday");
	}
	else if(day==3){
		printf(" is a Tuesday");
	}
	else if(day==4){
		printf(" is a Wednesday");
	}
	else if(day==5){
		printf(" is a Thursday");
	}
	else if(day==6){
		printf(" is a Friday");
	}
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS