Showing posts with label Week. Show all posts
Showing posts with label Week. Show all posts

C Program To Find Week Day Of A Given Date

How to write a C Program To Find Week Day Of A Given Date in C Programming Language ?


A real time Program is an Integration of all the constructs what we have learned until now. Hence if your practice on the previous sessions are practically proper then we can attempt any logic which cal make intelligent program. Let us take a test. Let us write a Program to find the WEEK DAY of a Given DATE.

Solution For C Program :

/*C Program To Find Week Day Of A Given Date*/

#include<stdio.h>
void main()
{
int d, m, y, n, nleap, nyear, nord, nodd1, nodd2, day, j, sum;
printf("\nEnter Any Date (DD-MM-YYYY) Format Only : ");
scanf("%d-%d-%d", &d, &m, &y);
printf("\n\nDate Entered is : %d - %d - %d", d, m, y);
if(y >= 1600 && y <= 1900)
{
day = 0;
nyear = y - 1600;
}
if(y >= 1901 && y <= 2400)
{
day = 1;
nyear = y - 1901;
}
nleap = nyear / 4;
nord = nyear - nleap;
nodd1 = 2 * nleap + 1 * nord;
day += nodd1 % 7;
sum = 0;
for(j = 1; j < m; j++)
{
switch(j)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
sum += 31;
break;
case 4: case 6: case9: case 11:
sum += 30;
break;
case 2:
if(y % 4 == 0)
sum += 29;
else
sum += 28;
}
}
sum += d;
nodd2 = sum % 7;
day += nodd2;
day %= 7;
switch(day)
{
case 0:
printf("\nDay is Sunday ");
break;
case 1:
printf("\nDay is Monday ");
break;
case 2:
printf("\nDay is Tuesday ");
break;
case 3:
printf("\nDay is Wednesday ");
break;
case 4:
printf("\nDay is Thursday ");
break;
case 5:
printf("\nDay is Friday ");
break;
case 6:
printf("\nDay is Saturday ");
break;
}
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Programming Question Solution

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Leap years: 1900-1904-1908..

1.  31 - January
2.  28/29 - Februar
3.  31- Mars
4.  30 - April
5.  31 - Mai
6.  30 - Juni
7.  31 - Juli
8.  31 - August
9.  30 - September
10. 31 - Oktober
11. 30 - November
12. 31 - Desember
101011010101

Solution:


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main()
  4. {
  5.         int year = 1901, month = 1, day = 2, sundays = 0, dayOfMonth=1, februaryLength= 28;
  6.         // Day = day of the week. 1 = Monday 2 = Tuesday .. 7 = Sunday
  7.         for (year = 1901; year <= 2000; year++) // Step through years
  8.         {
  9.                 for (month = 1; month <= 12; month++) // Step through months
  10.                 {
  11.                         if (day == 7) // Check if the first is a Sunday
  12.                         {
  13.                                 sundays++;
  14.                         }
  15.  
  16.                         // 31 DAY MONTH
  17.                         if ( (month == 1) || (month == 3) || (month == 5) || month == 7 || month == 8 || month == 10 || month == 12)
  18.                         {
  19.                                 for (dayOfMonth = 1; dayOfMonth <= 31; dayOfMonth++)
  20.                                 {
  21.                                         day++;
  22.                                         if (day == 8)
  23.                                                 day = 1;
  24.                                 }
  25.                         }
  26.                         else if (month == 4 || month == 6 || month == 9 || month == 11)
  27.                         {
  28.                                 for (dayOfMonth = 1; dayOfMonth <= 30; dayOfMonth++)
  29.                                 {
  30.                                         day++;
  31.                                         if (day == 8)
  32.                                                 day = 1;
  33.                                 }
  34.                         }
  35.  
  36.                         else if (month == 2) // Check if leap year
  37.                         {
  38.                                 if (year % 4 == 0) // Leap year
  39.                                 {
  40.                                         februaryLength = 29;
  41.                                 }
  42.                                 else
  43.                                         februaryLength = 28;
  44.                                 for (dayOfMonth = 1; dayOfMonth <= februaryLength; dayOfMonth++)
  45.                                 {
  46.                                         day++;
  47.                                         if (day == 8)
  48.                                                 day = 1;
  49.                                 }
  50.                         }
  51.                 }
  52.  
  53.         }
  54.         printf("\nNumber of sundays: %d", sundays);
  55.         system("PAUSE");
  56. }