Find out the day of the week from a given date.

         Day of week from a given date

Write a program that finds out the day of the week from a given date. The formula for calculating the day is-

day = (y + j + f - h + fh) % 7;

j = julian day of the date
y = year of given date (in 4 digit)
f = int part of (y-1)/4
h = int part of (y-1)/100
fh = int part of (y-1)/400
The value of  variable day tells us the day of week

Value of  the variable date

Name of day of  week

0

Saturday

1

Sunday

2

Monday

3

Tuesday

4

Wednesday

5

Thursday

6

Friday


In the above formula Julian day of a date represent the day of year on which the date fall. Julian day of the 1st Jan is 1 , of 2nd Feb is 33, of 31st Dec is 365 (366 if leap year).

Month

days

Not leap year

Leap year

January

31

31

31

February

28/29

32-59

32-60

March

31

60-90

61-91

April

30

91-120

92-121

May

31

121-151

122-152

June

30

152-181

153-182

July

31

182-212

183-213

August

31

213-243

214-244

September

30

244-273

245-274

October

31

274-304

275-305

November

30

305-334

306-335

December

31

335-365

336-366



Now let's decide the structure of our program. Once we get the value of variable day, we can use switch to print the name of day of the week from the value of  day.

To calculate the value of day we will have to find out the value of Julian day. Let's month and year of entered date are stored in the variable d, m, y respectively. We take another variable for value of  Julian day.
Initially the value of j is taken equal to d, and then days of previous month are added to j to get the value of Julian day.

suppose date is 29/05/2022
d = 29
m = 05
y = 2022
j = 29 + 31 + 28 + 31 + 30 = 149
if the year is leap then add 1 mean j = j+1;
The above logic applied in switch statement was simple but whole switch statement is confusing read and understand.

now it is easy how to find the day by using the above formula is -
day = (y + j + f - h + fh)%7
we have only y and j not f ,h,and fh
now find f = (y-1)/4
                 = (2022-1)/4
                = 2021/4
                = 505.25
but f is int so f = 505
similarly
h = (y-1)/100
h = (2022-1)/100
h = 20

fh = (2022-1)/400
fh = 2021/400
fh = 5

day = (2022 + 149 + 505 - 20 +5)%7
day = 2661%7
day = 1 mean (Sunday)

see in program :-
/* write a program that finds out the day of week from a given date*/
#include <stdio.h>
#include <conio.h>
int main()
{
    int d, m, y, j, f, h, fh, day;
    /* where
    d=date, m = month,y= year,j = julian day of the date,
    f= int part of (y-1)/4,h = int part of (y-1)/100, fh=int part of (y-1)/400
    julian day = 1,2,3,4,5,6,7,8,9,.............31,32,33,34........365/366*/
    printf("Enter the date (dd/mm/yyyy):\n");
    scanf("%d/%d/%d", &d, &m, &y);
    if (d < 0 && d >= 31 || m < 0 && m > 12)
    {
        printf("Invalid Date!");
    }
    else
    {
        j = d;
        if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
// for 366 days in the leap year.
        {
            if (m != 1 && m != 2) // not the month Jan and Feb.
            {
                j = j + 1;
            }
        }
        switch (m - 1)
        {
        case 11:
            j += 30; // ---> November month
do not use break statement b/c they can add the month also.
        case 10:
            j += 31; //---> October month
        case 9:
            j += 30; //---> September month
        case 8:
            j += 31; //--->  August month
        case 7:
            j += 31; //---> July month
        case 6:
            j += 30; //---> June month
        case 5:
            j += 31; //---> May month
        case 4:
            j += 30; //---> April month
        case 3:
            j += 31; //---> March month
        case 2:
            j += 28; //---> February month
        case 1:
            j += 31; //---> January month
        }
    }
    f = (y - 1) / 4;
    h = (y - 1) / 100;
    fh = (y - 1) / 400;
    // formula to calculate the day
    day = (y + j + f - h + fh) % 7;
    switch (day)
    {
    case 0:
        printf("Saturday");
        break;
    case 1:
        printf("Sunday");
        break;
    case 2:
        printf("Monday");
        break;
    case 3:
        printf("Tuesday");
        break;
    case 4:
        printf("Wednesday");
        break;
    case 5:
        printf("Thursday");
        break;
    case 6:
        printf("Friday");
        break;
    }
return 0;
}
Output :
Enter the date (dd/mm/yyyy): 29/05/2022 Sunday



***** Thank You ******

Comments

Popular posts from this blog

18 Amazing programs in C.