ADVERTISEMENT
Get Day Name From Date
This programme takes user input for a day, month, and year and calculates the corresponding day of the week using Zeller’s Congruence.
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
char* months[] = {"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan","Feb"};
char* days[]= { "sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };
main()
{
int k,m,D,C,year,i,f,finalday,flag=0;
char str[3];
printf("Enter date \n");
scanf("%d",&k);
if(k<=0||k>31)
{
printf("Invalid Date\n");
exit(0);
}
printf("Enter first 3 letters of month of the year ex:-for January enter Jan\n");
scanf("%s",str);
for(i=0; i<12; i++)
{
if(!strcmp(str,months[i]))
{
m=i+1;
flag=1;
break;
}
}
if(flag==0)
{
printf("Invalid Month\n");
exit(0);
}
printf("Enter year\n");
scanf("%d",&year);
if(m==11||m==12)
{
year=year-1;
}
D=year%100;
C=year/100;
f = (k+(((13*m)-1)/5)+D+(D/4)+(C/4))-(2*C);
if(f>=0)
{
finalday=f%7;
}
else
{
finalday=((f%7)+7)%7;
}
printf("The Day for given %d,%s %d is\n%s\n",k,str,year,days[finalday]);
}C Programming ExamplesEnter date
25
Enter first 3 letters of month of the year ex:-for January enter Jan
Dec
Enter year
2021
The Day for given 25,Dec 2021 is
saturday
C Programming ExamplesCertainly! Let’s break down the provided C code step by step:
- Include Header Files:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
These are standard C library header files. <stdio.h> is for input/output functions, <stdlib.h> for memory allocation functions, and <string.h> for string manipulation functions.
- Declare Arrays:
char* months[] = {"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan","Feb"};
char* days[] = {"sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
Two arrays are declared:
months[]: Stores the three-letter abbreviations of months.days[]: Stores the names of days of the week.
- Main Function:
main()
{
In older C standards, the main() function is used without specifying the return type (implicitly considered int).
- Declare Variables:
int k, m, D, C, year, i, f, finalday, flag = 0;
char str[3];
Variables are declared to store day, month, year, loop index, and flags to handle errors.
- User Input:
printf("Enter date \n");
scanf("%d", &k);
The user is prompted to input the day of the month.
- Check for Valid Date:
if (k <= 0 || k > 31)
{
printf("Invalid Date\n");
exit(0);
}
Checks if the entered day is within a valid range (1 to 31).
- User Input (Month):
printf("Enter first 3 letters of month of the year ex:-for January enter Jan\n");
scanf("%s", str);
The user is prompted to input the first three letters of the month.
- Check for Valid Month:
for (i = 0; i < 12; i++)
{
if (!strcmp(str, months[i]))
{
m = i + 1;
flag = 1;
break;
}
}
Compares the entered month abbreviation with the predefined list. If a match is found, sets m to the corresponding month number.
- Handle Invalid Month:
if (flag == 0)
{
printf("Invalid Month\n");
exit(0);
}
If no match is found, prints an error message and exits.
- User Input (Year):
printf("Enter year\n");
scanf("%d", &year);
The user is prompted to input the year.
- Adjust Year for Months Jan and Feb:
if (m == 11 || m == 12)
{
year = year - 1;
}
If the month is November or December, adjust the year for the calculation.
- Adjust Year for Months Jan and Feb:
- Handle Negative Result:
if (f >= 0)
{
finalday = f % 7;
}
else
{
finalday = ((f % 7) + 7) % 7;
}
If the result is negative, adjust it to a positive value within the range [0, 6].
- Print Result:
printf("The Day for given %d,%s %d is\n%s\n", k, str, year, days[finalday]);
Print the calculated day of the week based on the input.
- Close Main Function:
}
Close the main function.
This programme takes user input for a day, month, and year and calculates the corresponding day of the week using Zeller’s Congruence. It includes checks for valid input and handles edge cases for adjusting the year. The final result is printed on the console.