Tuesday, May 14, 2019

Day by Date

  • Problem Description

    "Computation of the date either previous or forthcoming dates is quiet easy. But it is quiet difficult to calculate the day from a particular given date. You are required to find a day from a particular date given to you.
    Input
    It consists of a single line entry consisting of date in format dd mm yyyy. i.e. the input line consists of the three numbers written in order followed by spaces. Eg. Input for 18-12-1990 is be written as 18 12 1990
    Output
    It consists of single line output showing the day for that particular date.
    "
  • Test Case 1

    Input (stdin)
    14 3 2012

    Expected Output
    Wednesday
  • Test Case 2

    Input (stdin)
    25 01 2011

    Expected Output
    Tuesday

Solution

#include<stdio.h>
int main()
{
int month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},i,d,m,y;
char* day[]= {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            scanf("%d %d %d", &d, &m, &y);
    if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
    month[1] = 29;
     
    if (y < 1900 || m < 1 || m > 12 || d < 1 || d > month[m - 1]) {
    // printf("This is an invalid date.\n");
    return 1;
    }
     
    for (i = 1900; i < y; i++)
    if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
    d += 366;
    else
    d += 365;
     
    for (i = 0; i < m - 1; i++)
    d += month[i];
     
    printf("%s\n", day[d % 7]);
    return 0;
}
 

3 comments:

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...