mardi 23 mars 2021

In this problem I can't understand why there should be -1 in (diff=yr-1900-1)?

I copied this code from internet after not getting the output right. The following code works fine but I don't have a clue why there is a subtraction of -1 in diff variable.

/*To find day of 1st January of a year*/
/*Given that 01/01/1900 is Monday*/
/*Author Anjisht Amritanshu*/
/*Date: 23 March 2021*/
#include<stdio.h>
int main()
{
        int yr,diff,leap,normal,totday,day;

        printf("Enter year=");
        scanf("%d",&yr);

        diff=yr-1900-1; /*Can't understand why here -1 should be included*/
        leap=diff/4;
        normal=diff-leap;

        totday=(365*normal)+(366*leap)+1;

        day=totday%7;

        if(day==0)
                printf("Monday");
        if(day==1)
                printf("Tuesday");
        if(day==2)
                printf("Wednesday");
        if(day==3)
                printf("Thursday");
        if(day==4)
                printf("friday");
        if(day==5)
                printf("Saturday");
        if(day==6)
                printf("Sunday");
}

Aucun commentaire:

Enregistrer un commentaire