ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Formula for calculating the day of week from a given date

Updated on February 23, 2015

The formula for calculating the day is-

day=(y + j + a - b + ab) %7

j = julian day of the date

y = year of given date (in 4 digits)

a = int part of (y-1)/4

b = int part of (y-1)/100

ab=int part of (y-1)/400

The value of variable day tells us the day of week

Value of variable day
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 represents the day of year.

Julian day of:

1st Jan = 1

5th Feb = 36

31st Dec = 365 (366 if leap year)

Code (In 'C' Language)

#include<stdio.h>
#include<conio.h>
void main()
{
   int d,m,y,j,a,b,ab,day;
   printf("Enter date (dd/mm/yyyy) : ");
   scanf("%d/%d/%d",&d,&m,&y);
   j=d;
   switch(m-1)
    {
      case 11:
        j+=30;
      case 10:
        j+=31;
      case 9:
        j+=30;
      case 8:
        j+=31;
      case 7:
        j+=31;
      case 6:
        j+=30;
      case 5:
        j+=31;
      case 4:
        j+=30;
      case 3:
        j+=31;
      case 2:
        j+=28;
      case 1:
        j+=31;
    }
    if(y%4==0 && y%100 !=0 || y%400==0)
        if(m!=1&&m!=2)
           j=j+1;

    a=(y-1)/4;
    b=(y-1)/100;
    ab=(y-1)/400;
    day=(y+j+a-b+ab)%7;
    switch(day)
     {
       case 0:
         printf("Saturday\n");
         break;
       case 1:
         printf("Sunday\n");
         break;
       case 2:
         printf("Monday\n");
         break;
       case 3:
         printf("Tuesday\n");
         break;
       case 4:
         printf("Wednesday\n");
         break;
       case 5:
         printf("Thursday\n");
         break;
       case 6:
         printf("Friday\n");
         break;
     }
 }

Program to find the LCM and HCF of two numbers

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,c,d;
  printf("Enter 1st Number: ");
  scanf("%d",&a);
  printf("Enter 2nd Number: ");
  scanf("%d",&b);
  c=a;
  d=b;
   while(c!=d)
    {
      if(c<d)
        c=c+a;
      else
        d=d+b;
    }
    printf("LCM of %d and %d is %d\n",a,b,c);
    c=a;
    d=b;
    while(c!=d)
     {
       if(c>d)
         c=c-d;
       else
         d=d-c;
     }
     printf("HCF of %d and %d is %d\n",a,b,c);
}

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      reader 21 months ago

      The running time of the program can be optimized. Note that HCF x LCM of two numbers is the product of them. Calculation HCF requires less computation than calculating the LCM, you could then extend the program to find the LCM and HCF of arbitrary n input numbers. The calendar program too needs optimization.

    Click to Rate This Article