Advertisement
Noam_15

sum_days_from_start_of_year

Apr 1st, 2020
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void main () {
  4.     int inMonth;
  5.     int inYear;
  6.     int inDay;
  7.     int daysPassedFromStartOfYear = 0;
  8.     int currMonth;
  9.  
  10.     printf("Enter year: ");
  11.     scanf("%d", &inYear);
  12.  
  13.     printf("Enter month: ");
  14.     scanf("%d", &inMonth);
  15.  
  16.     printf("Enter day: ");
  17.     scanf("%d", &inDay);
  18.  
  19.     // Sum days of all months before the current month
  20.     for (currMonth = 1; currMonth < inMonth; currMonth++) {
  21.         daysPassedFromStartOfYear += GetDaysInMonth(inYear, currMonth);
  22.     }
  23.  
  24.     // Add days passed this month
  25.     daysPassedFromStartOfYear += inDay;
  26.  
  27.     printf("Days passed: %d\n", daysPassedFromStartOfYear);
  28. }
  29.  
  30. int GetDaysInMonth(int year, int month) {
  31.     int days = 0;
  32.  
  33.     switch( month ) {
  34.         case 1:
  35.         case 3:
  36.         case 5:
  37.         case 7:  
  38.         case 8:
  39.         case 10:
  40.         case 12:
  41.             days = 31;
  42.             break;
  43.         case 4:
  44.         case 6:
  45.         case 9:
  46.         case 11:
  47.             days = 30;
  48.             break;
  49.         case 2:
  50.             if (year % 4 == 0)
  51.                 days = 29;
  52.             else
  53.                 days = 28;
  54.             break;
  55.         default:
  56.             days = 0;
  57.             break;
  58.     }
  59.  
  60.     return days;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement