top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Finding day of a given date using C, can someone help me with the code and algorithm?

+2 votes
372 views
Finding day of a given date using C, can someone help me with the code and algorithm?
posted Feb 6, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Code to get day of given date in c language

#include <stdio.h>
#include<string.h>
int 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 ;
 strcpy(week[0], "Saturday") ;
 strcpy(week[1], "Monday") ;
 strcpy(week[2], "Tuesday") ;
 strcpy(week[3], "Wednesday") ;
 strcpy(week[4], "Thursday") ;
 strcpy(week[5], "Friday") ;
 strcpy(week[6], "Sunday") ;
 printf("Date format (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("\nDay =%s", week[s]) ;
 return 0;
}

Algorithm
1. Input Date in format : dd/mm/yyyy
2.check if leap year or not
3.if yes
then add February month to 29 days
else
set it to 28 days which is default set.
4.for each month calculate the value of s,
s = s + month[i] ;
s = s + (date + year + (year / 4) - 2) ;
s = s % 7
5. print the Day of given date.

answer Feb 6, 2016 by Shivam Kumar Pandey
Similar Questions
+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

+2 votes

I am looking for sample code which can impress an interviewer.

+1 vote

loop for all S in this F
find each S highest value
keep this in a hashset1 (key=S, value= max value)
order each S value in increasing order
keep this in another hashsset2 (key = S, value = list of values)
end loop for all S

order hashset1 in increasing order

loop hashset1
pick up each S
loop hashset2

pick up each element (roll no, plies, values)
show that on screen

end loop hashset2

end loop hashset1

// Code is most welcome... Literally have worked with this but couldn't break the jinx with their key-value.

...