bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc018-logic.c (3467B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 12 Dec 2025
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* According to Gregorian calender, it was Monday on the date 01/01/01.
      9 Write a program to find out what is the day on 1st January of any input year. */
     10 /* Let Us C, Chap- 3, Page - 53, Qn No.: f(i) */
     11 
     12 #include <stdio.h>
     13 
     14 /**
     15  * @brief Determines if a given year is a leap year.
     16  * * The rule: A year is a leap year if it is divisible by 4, UNLESS it is 
     17  * divisible by 100 but NOT by 400.
     18  * * @param year The year to check.
     19  * @return 1 if it is a leap year, 0 otherwise.
     20  */
     21 int is_leap(int year) {
     22     // Check if divisible by 400 OR (divisible by 4 AND not divisible by 100)
     23     if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
     24         return 1;
     25     }
     26     return 0;
     27 }
     28 
     29 /**
     30  * @brief Calculates the day of the week for January 1st of the given year.
     31  * * The base date is 01/01/01, which was a Monday (index 1).
     32  * * Day Mapping: 0:Sunday, 1:Monday, 2:Tuesday, 3:Wednesday, 4:Thursday, 5:Friday, 6:Saturday
     33  */
     34 int main() {
     35     long long year; // Use long long for year input if years far in the future/past are tested
     36     int i;
     37     long long total_days = 0;
     38     int day_index;
     39     
     40     // Day names array for output
     41     const char *day_names[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     42 
     43     printf("Enter the year (e.g., 2025): ");
     44     if (scanf("%lld", &year) != 1 || year < 1) {
     45         printf("Invalid year input. Please enter a positive integer year (>= 1).\n");
     46         return 1;
     47     }
     48 
     49     // --- Core Logic: Calculate Total Days ---
     50     
     51     // We only need to consider the years that have *passed* before the target year.
     52     // So, we count days from the end of year 0 up to the end of year (year - 1).
     53     int years_passed = year - 1;
     54     
     55     // 1. Calculate the number of leap days up to the end of year (year - 1)
     56     // Formula based on Gregorian calendar rules for years Y-1:
     57     // (Y-1)/4 - (Y-1)/100 + (Y-1)/400
     58     long long leap_years = years_passed / 4 - years_passed / 100 + years_passed / 400;
     59 
     60     // 2. Total days = (Number of years * 365) + (Number of leap years)
     61     // Note: The loop method (below) is more intuitive but the formula is faster.
     62     // We will use the direct formula for efficiency.
     63     total_days = years_passed * 365 + leap_years;
     64 
     65     // --- Alternate Loop Method (for conceptual simplicity) ---
     66     /*
     67     for (i = 1; i < year; i++) {
     68         total_days += 365;
     69         if (is_leap(i)) {
     70             total_days += 1; // Add 1 for the leap day
     71         }
     72     }
     73     */
     74     
     75     // --- Determine the Day of the Week ---
     76     
     77     // Since 01/01/01 was Monday (index 1), we use the following setup:
     78     // Index 1 corresponds to Monday.
     79     // The calculation gives the number of days *past* the Monday start (01/01/01).
     80     // The modulo operation gives the remainder (0-6).
     81     
     82     // 0 days elapsed (Year 1): total_days=0. (0 + 1) % 7 = 1 (Monday). Correct.
     83     // 365 days elapsed (Year 2): total_days=365. (365 + 1) % 7 = 2 (Tuesday). Correct. (365 mod 7 = 1, 1+1 = 2)
     84     
     85     day_index = (total_days + 1) % 7;
     86     
     87     // Correct the Day Index to match the array (0:Sun, 1:Mon, ..., 6:Sat)
     88     // The +1 adjusts for the Monday starting point (index 1).
     89     
     90     printf("\nOn January 1st, %lld, the day was: **%s**.\n", year, day_names[day_index]);
     91 
     92     return 0;
     93 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror