Here’s a C program to find the day of the given date with output and proper explanation. The program takes as input a formatted date and returns the day on that date. This program makes use of single and multidimensional arrays, for loop and string function - strcpy() (string copy).
# include <stdio.h> # include <conio.h> void main() { int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; char week[7][10] ; int date, mon, year, i, r, s = 0 ; clrscr(); strcpy(week[0], "Sunday") ; strcpy(week[1], "Monday") ; strcpy(week[2], "Tuesday") ; strcpy(week[3], "Wednesday") ; strcpy(week[4], "Thursday") ; strcpy(week[5], "Friday") ; strcpy(week[6], "Saturday") ; printf("Enter a valid date (dd/mm/yyyy) : ") ; scanf("%d / %d / %d", &date, &mon, &year) ; if( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ) month[1] = 29 ; for(i = 0 ; i < mon - 1 ; i++) s = s + month[i] ; s = s + (date + year + (year / 4) - 2) ; s = s % 7 ; printf("\nThe day is : %s", week[s]) ; getch() ; }
Output of above program is
Enter a valid date (dd/mm/yyyy) : 02/11/1977
The day is : Wednesday
Explanation of above program
In this program we have several variables let take a look at each first -
- month[12] - This is an array of type integer and its size is 12 i.e. 12 different integers can be stored in this array. This array is also initialized at the time of declaration. Each value of this array represents the number of days in a particular month e.g. first value 31 represents that January has 31 days.
- char[7][10] - This is another array but unlike the above month[12] array, this array is a multidimensional array of the type character. This array can have 7 values and length of each value must be less than or equal to 10. This array represents the days e.g. Sunday, Monday and so on.
- date, month and year - These are integers and are used to store date, month and year entered by the user.
- i, r and s - This are some other integers used for intermediate calculations.
Note: Here, rather than initializing the char[7][10] array at the time of declaration like we did with month[12] array, we’re making use of C language’s string copy function strcpy().
With the help of strcpy() function, week[7][10] is initialized. Now we ask the user to enter the input in a preformatted manner. Notice inside scanf() we’ve separated each integer by a “/”, so when user enters the date in a format like dd/mm/yyyy all three values are automatically get stored in their respective variable.
Next, the if condition is for checking whether the year that user entered is a leap year or not. If the year is indeed a leap year the number of days in the February month is set to 29 by month[1] = 29;
Remember: The index of an array starts at 0, so in order to access the second value we have to refer to the index 1. That’s what we did here in the above expression - month[1] = 29
After checking for the leap year, we enters inside a for loop. The for loops runs for the values of loop variable i = 0 to mon - 2 (in loop i < mon - 1), adding all the days of months that are before the month that user entered to the variable s e.g. suppose user entered mon = 3, so the for loop will run for i = 0 to 1. So s = s + month[i] or in this case s = month[0] + month[1].
After that outside the loop, we further update the value of s by -
s = s + (date + year + (year / 4) - 2) ; and then s = s % 7.
After the above process, s will contain an index to the week array which can be accessed as - week[s] and this value shows the day on the given date.
Note: “%s” in printf() is used to print the string value of week[s].
could you please explain what happens in this line
s = s + (date + year + (year / 4) - 2) ; and then s = s % 7. ???
This is the key to calculate Day of the week. For more information please refer http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
Your program gives wrong answer for 29/2/2012.
Your code is giving thursday actually it's wednesday.
You can insert below line after line 23 in your code:
if(the_year_is_leap_year)
s=s-1;
@Kbiiit: Thanks for pointing out the issue. The logic to determine leap year written in line no 18 has been corrected. Please try this updated code.
hi admin i want to solve this..... assume tat starting day and date of a year s sunday and 1.v need 2 find the day for a given date. (input=10 & output = tuesday).reply me
Your program gives wrong output for 29/2/2012 and 4/1/2012
if(date1>0 && date1<=31 && month1>0 && month1<=12 )
{
if( (year1 % 400 == 0) || ((year1 % 4 == 0) && (year1 % 100 != 0)) )
{
month[1]=29;
for(i = 0 ; i < month1-1 ; i++)
{
s = s + month[i] ;
s = s + (date1 + year1 + (year1 / 4) - 2) ;
s = s % 7 ;
}
printf("\nThe day is : %s", week[s-1]) ;
}
else
{
if(month[0]<=31 || month[1]<=28 || month[2]<=30 || month[3]<=31 || month[4]<=30 || month[5]<=31 || month[6]<=30 || month[7]<=31 || month[8]<=30 || month[9]<=31 || month[10]<=30 || month[11]<=31 )
{
for(i = 0 ; i < month1 - 1 ; i++)
s = s + month[i] ;
s = s + (date1 + year1 + (year1 / 4) - 2) ;
s = s % 7 ;
printf("\nThe day is : %s", week[s]) ;
}
}
}
It not giving correct when i giving 9/2/2016 it shows wednesday but o/p should be tuesday
/*FIND DAY*/
#include //HEADER FILE
#include //HEADER FILE
main() //FUNCTION MAIN
{
int y,mon,d,c; //VARIABLE DECLARATION
int s=0,i; //VARIABLE DECLARATION
int ordinary[]={31,28,31,30,31,30,31,31,30,31,30,31}; //ARRAY DECLARATION
printf("\nENTER YEAR\n\n"); //PRINT STATEMENT
scanf("%d",&y); //VALUE ENTER THROUGH KEYBOARD
printf("\nENTER MONTH\n\n"); //PRINT STATEMENT
scanf("%d",&mon); //VALUE ENTER THROUGH KEYBOARD
printf("\nENTER DATE\n\n"); //PRINT STATEMENT
scanf("%d",&d); //VALUE ENTER THROUGH KEYBOARD
if(y%400==0&&y%4==0) //CONDITION FOR LEAP YEAR
{
ordinary[1]=29; //INITIALIZE VALUE OF ARRAY ONE
}
for(i=0;i<mon-1;i++) //LOOP COUNT VALUE OF M & -1
s=s+ordinary[i]; //ARRAY
s=s+(d+y+(y/4)-2); //ADD VALUE OF S IN OTHER EQUATIONS
s=s%7; //VALUE OD S MOD BY 7
if(s==0) //CONDITION
{
printf("\n\nSUNDAY\n\n"); //PRINT STATEMENT
}
if(s==1) //CONDITION
{
printf("\n\nMONDAY\n\n"); //PRINT STATEMENT
}
if(s==2) //CONDITION
{
printf("\n\nTUESDAY\n\n"); //PRINT STATEMENT
}
if(s==3) //CONDITION
{
printf("\n\nWEDNESDAY\n\n"); //PRINT STATEMENT
}
if(s==4) //CONDITION
{
printf("\n\nTHURSDAY\n\n"); //PRINT STATEMENT
}
if(s==5) //CONDITION
{
printf("\n\nFRIDAY\n\n"); //PRINT STATEMENT
}
if(s==6) //CONDITION
{
printf("\n\nSATURDAY\n\n"); //PRINT STATEMENT
}
getch() ; //SCREEN HOLDING
}
#include
int main()
{
int dd,mm,yyyy,mon[12]={31,28,31,30,31,30,31,31,30,31,30,31},sum=0,i,day,ly=0;
abc:
mon[1]=28;
printf("Enter correct date\n");
scanf("%d/%d/%d",&dd,&mm,&yyyy);
if(yyyy%400==0||(yyyy%4==0 && yyyy%100!=0))
mon[1]=29;
if(mm>12 || dd>31)
{
printf("Wrong input\n");
goto abc;
}
if(mm==2 && dd>29)
{
printf("february can't be more than 29\n");
goto abc;
}
if(mm==2 && mon[1]==28 && dd>28)
{
printf("In year %d can't have feb. greater than 28\n",yyyy);
goto abc;
}
if((mm==4 || mm==6 || mm==9 || mm==11) && dd>30)
{
printf("day can't be more than 30 in month=%d\n",mm);
goto abc;
}
for(i=0;i<mm-1;i++)
sum=sum+mon[i];
for(i=1;i<yyyy;i++)
{
if(i%400==0||(i%4==0 && i%100!=0))
ly++;
}
sum=sum+dd+((yyyy-1)*365)+ly;
day=sum%7;
printf("Day on %d/%d/%d is : ",dd,mm,yyyy);
switch(day)
{
case 0: printf("Sunday");break;
case 1: printf("Monday");break;
case 2: printf("Tuesday");break;
case 3: printf("Wednesday");break;
case 4: printf("Thursday");break;
case 5: printf("Friday");break;
case 6: printf("Saturday");break;
default : printf("Error");break;
}
return 0;
}
s = s + (date1 + year1 + (year1 / 4) - 2) ;
why -2 there
If I enter a date 23/03/2000 why it don't show correct day i.e.thursday it shows incorrect day friday
No
No🚫
s = s + (date1 + year1 + (year1 / 4) - 2) ;
Above line -What is it mean?
You have to give 09/02/2016
If I enter 10/06/1747 its shows Thursday.
But the actual day is Saturday
Condition for leap year is wrong right condition is ((year % 400==0 || year % 100 ==0)&&(year % 4 == 0)
You could've done this:
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* week[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Can't it be just year%4==0?
The advantage of JavaScript is that Client-side JavaScript is very fast because it can be run immediately within the client-side browser. Unless outside resources are required, JavaScript is unhindered by network calls to a backend server. JavaScript is relatively simple to learn and implement. Whether you are looking to market a new business through JavaScript Programming or become a professional JavaScript developer Live Training Lab has a course to help you achieve your goals. free C++ Courses
It gives wrong output for 10/07/2004
It's Saturday but it showing Sunday
This is the fitting weblog for anybody who needs to find out about this topic. You understand a lot its almost onerous to argue with you (not that I truly would need…HaHa). You definitely put a new spin on a subject thats been written about for years. Nice stuff, simply great! programar en java
Inovasi dan Keberlanjutan: maklon kosmetik bogor untuk Bisnis Kecantikan yang Sukses.
Improve your website's loading speed and performance to enhance its Kimtoto optimization. Kimtoto
Your website has reminded me of the beauty of simplicity and the joy of living in the moment. Kimtoto
Jelajahi fitur baru dan penawaran menarik setiap hari di Situs IDN Pokercapsa Online 2024. Situs IDN Pokercapsa Online 2024
Dapatkan pemadam kebakaran dengan garansi kualitas dari distributor alat pemadam kebakaran terpercaya. distributor alat pemadam kebakaran
Agen X500 membawa Anda ke dunia permainan yang penuh dengan kesenangan dan keuntungan agen x500
Dapatkan topup game murah dengan proses cepat dan aman di website kami website topup game murah
Mainkan slot dengan berbagai jenis payline di master198
ludotogel menawarkan hadiah besar dalam permainan togel
Dapatkan pengalaman bermain game yang menyenangkan di Hokiwin
This platform offers endless possibilities for making money while playing your favorite games. The opportunities here are vast, and it's highly recommended for anyone looking to monetize their gaming skills! empiretoto
Indulge in a variety of games with amazing promotions and bonuses, and experience the easiest mobile login on the official site! Dutaslot
Find the most dazzling game site with advanced graphics and smooth login, and download an unblockable mobile APK for ultimate gaming! Dewa99
A delightful online experience, very enjoyable. agen judidadu88
A great resource for knowledge bandar toto macau.
The articles here are always so well thought out mega888 apk.
The posts here are always thought-provoking and well-crafted. sojuslot
Nikmati grafis menakjubkan dan animasi halus dalam permainan slot gacor yang selalu gacor slot gacor
Thanks for the fantastic insights and well-written posts turbowin77