/********************************************** * Section : 10 * Roll No. : 20CS10000 / 20CS30000 * Name : Aritra Hazra * Assignment No : 2 * Description : Date Arithmetic Operations and Date Difference Calculator * Date : 13-Apr-2021 **********************************************/ #include int main() { int startDate, startMonth, startYear; int endDate, endMonth, endYear; unsigned long long int dateDiff; unsigned int startDay, dDiff, mDiff, yDiff; // taking as input two dates from user printf("**** User Input of Date Details ****\n"); printf("\t++ Enter First (earlier) Date (DD/MM/YYYY format): "); scanf("%d/%d/%d", &startDate, &startMonth, &startYear); printf("\t++ Enter Second (later) Date (DD/MM/YYYY format): "); scanf("%d/%d/%d", &endDate, &endMonth, &endYear); printf("\t++ Enter the Day of the Week for First Date (0-6 for Sun-Sat): "); scanf("%u", &startDay); // calculating date difference if(endDate >= startDate) { // end date is beyond start date dateDiff = endDate - startDate; } else { // end date is less than start date -- borrow days of next month dateDiff = endDate; if(startMonth <= 6) { // adding 31 day months dateDiff = dateDiff + (31 - startDate); } else { // adding 30 day months dateDiff = dateDiff + (30 - startDate); if((startMonth == 11) && !((startYear%400 == 0) || ((startYear%100 != 0) && (startYear%4 == 0)))) { // adjusting non-leap year month having 29 days dateDiff = dateDiff - 1; } } // start month incremented due to days of one month borrowed while subtraction if(startMonth == 12) { startMonth = 1; startYear = startYear + 1; } else { startMonth = startMonth + 1; } } dDiff = dateDiff; // keeping the date difference // calculating month difference if(endMonth >= startMonth) { // end month is beyond start month mDiff = (endMonth - startMonth); // keeping month difference if(endMonth <= 7) { // adding 31 day months dateDiff = dateDiff + mDiff*31; } else { // adding 30 day months dateDiff = dateDiff + mDiff*30; if(startMonth <= 6) { dateDiff = dateDiff + (7 - startMonth); } if(!((startYear%400 == 0) || ((startYear%100 != 0) && (startYear%4 == 0))) && (endMonth == 12) && (startMonth <= 11)) { // adjusting non-leap year month having 29 days dateDiff = dateDiff - 1; } } } else { // end month is less than start month -- borrow days of next year mDiff = 12 + (endMonth - startMonth); // keeping month difference borrowing 12 months from next year if(startMonth <= 6) { // start month is within 31 day months zone dateDiff = dateDiff + (7-startMonth)*31 + 6*30; } else { // start month is within 30 day months zone dateDiff = dateDiff + (13-startMonth)*30; } if((startMonth < 12) && !((startYear%400 == 0) || ((startYear%100 != 0) && (startYear%4 == 0)))) { // adjusting non-leap year month having 29 days dateDiff = dateDiff - 1; } if(endMonth <= 6) { // end month is within 31 day months zone dateDiff = dateDiff + (endMonth-1)*31; } else { // end month is within 30 day months zone dateDiff = dateDiff + (endMonth-7)*30 + 6*31; } if((endMonth == 12) && !((startYear%400 == 0) || ((startYear%100 != 0) && (startYear%4 == 0)))) { // adjusting non-leap year month having 29 days dateDiff = dateDiff - 1; } // start year incremented due to days of one month borrowed while subtraction startYear = startYear + 1; } // calculating year difference yDiff = (endYear - startYear); if(yDiff > 0){ // computing days w.r.t. years only if years are apart (not-same) dateDiff = dateDiff + yDiff*365; // adding 365 days years // adding additional days for leap-years if(endMonth < 12){ // end year not to be counted for leap-year day dateDiff = dateDiff + ((endYear-1)/4 - (startYear-1)/4); } else{ // end year leap-year day to be counted dateDiff = dateDiff + (endYear/4 - startYear/4); } // subtracting extra-added days for non-leap years // (those are divisible by 100 but not divisible by 400) // FIRST: subtract -1 for all 100 divisible years if(endMonth < 12){ // end year not to be discounted for leap-year day dateDiff = dateDiff - ((endYear-1)/100 - (startYear-1)/100); } else{ // end year leap-year day to be discounted dateDiff = dateDiff - (endYear/100 - startYear/100); } // NEXT: add +1 for all 400 divisible years (due to previous additional subtractions made) if(endMonth < 12){ // end year not to be counted for leap-year day dateDiff = dateDiff + ((endYear-1)/400 - (startYear-1)/400); } else{ // end year leap-year day to be counted dateDiff = dateDiff + (endYear/400 - startYear/400); } } printf("\n**** The Date Difference ****"); // printing the date difference in terms of number of days printf("\n\t++ In DAYS (only): %llu days", dateDiff); // printing the date difference in terms of number of years, months and days printf("\n\t++ With BREAK-UP: %u years, %u months, %u days", yDiff, mDiff, dDiff); // printing the date difference in terms of number of weeks and days printf("\n\t++ In WEEK-DAYS: %llu weeks and %llu days", dateDiff/7, dateDiff%7); printf("\n\n**** The Second Date Status ****"); printf("\n\t++ The Day of the Week: "); switch((dateDiff%7 + startDay)%7) { // calculating the week-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; } printf("\n\t++ The Name of the Month: "); switch(endMonth) { // printing the month name case 1: printf("Vaisakha"); break; case 2: printf("Jyeshtha"); break; case 3: printf("Ashadha"); break; case 4: printf("Shraavana"); break; case 5: printf("Bhadra"); break; case 6: printf("Ashwina"); break; case 7: printf("Kartika"); break; case 8: printf("Agrahayana"); break; case 9: printf("Pausha"); break; case 10: printf("Magha"); break; case 11: printf("Phalguna"); break; case 12: printf("Chaitra"); break; } printf("\n\t++ The Year Characteristics: "); if(((endYear%400 == 0) || ((endYear%100 != 0) && (endYear%4 == 0)))) { // check for leap-year printf("LEAP-YEAR!"); } else { // not a leap-year printf("NOT LEAP-YEAR!"); } printf("\n\t++ The Year in English: "); if(endYear == 0) { // end year is zero printf("Zero"); } else { // non-zero end year switch(endYear/1000) { // end year is beyond 1000 case 1: printf("One "); break; case 2: printf("Two "); break; case 3: printf("Three "); break; case 4: printf("Four "); break; case 5: printf("Five "); break; case 6: printf("Six "); break; case 7: printf("Seven "); break; case 8: printf("Eight "); break; case 9: printf("Nine "); break; default: break; // do nothing } if(endYear/1000 > 0) { printf("Thousand "); } switch((endYear%1000)/100) { // the hundred-th part of end year case 1: printf("One "); break; case 2: printf("Two "); break; case 3: printf("Three "); break; case 4: printf("Four "); break; case 5: printf("Five "); break; case 6: printf("Six "); break; case 7: printf("Seven "); break; case 8: printf("Eight "); break; case 9: printf("Nine "); break; default: break; // do nothing } if((endYear%1000)/100 > 0) { printf("Hundred "); } if((endYear%100)/10 == 1){ /* YY within 10-19 */ switch (endYear%10){ /* word generation for 10-19 */ case 0: printf("Ten"); break; case 1: printf("Eleven"); break; case 2: printf("Twelve"); break; case 3: printf("Thirteen"); break; case 4: printf("Forteen"); break; case 5: printf("Fifteen"); break; case 6: printf("Sixteen"); break; case 7: printf("Seventeen"); break; case 8: printf("Eighteen"); break; case 9: printf("Nineteen"); break; } } else { /* YY other than 10-19 */ switch ((endYear%100)/10){ /* word generation for decimal part */ case 2: printf("Twenty "); break; case 3: printf("Thirty "); break; case 4: printf("Forty "); break; case 5: printf("Fifty "); break; case 6: printf("Sixty "); break; case 7: printf("Seventy "); break; case 8: printf("Eighty "); break; case 9: printf("Ninety "); break; default: /* do nothing */ break; } switch (endYear%10) { /* word generation for unitary part */ case 0: printf("\b"); break; case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; case 4: printf("Four"); break; case 5: printf("Five"); break; case 6: printf("Six"); break; case 7: printf("Seven"); break; case 8: printf("Eight"); break; case 9: printf("Nine"); break; } } } printf("\n"); return 0; }