Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BibTeX 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.    /* declare variables */
  6.    int month, day, year, month_back, day_back, year_back, i;
  7.    
  8.    /* start loop, set i = 0, continue loop if month/day/year are not 0, increment i after each run */
  9.    for(i = 0 ; month != 0 && year != 0 && day != 0 ; i++) {
  10.          
  11.          /* take date and store date into variables month/day/year */
  12.          printf("Enter a date mm/dd/yy: ");
  13.          scanf("%2d/%2d/%2d", &month, &day, &year);
  14.          
  15.          /* if i = 0(first run) make back ups of the original date */
  16.          if (i = 0) {
  17.                month_back = month;
  18.                day_back = day;
  19.                year_back = year;
  20.                }
  21.                
  22.                /* if this is not the first run (i != 0) */
  23.                else if (i != 0) {
  24.                    
  25.                     /* check if the new year entered is less than the previous year entered
  26.                     if yes then set the back ups to the current date */
  27.                      if (year < year_back) {
  28.                               year_back = year;
  29.                               day_back = day;
  30.                               month_back = month;
  31.                               }
  32.                              
  33.                               /* if the years are the same*/
  34.                               else if (year == year_back) {
  35.                                    
  36.                                    /* check if the new month is before the previously entered month */
  37.                                    if (month < month_back) {
  38.                                              year_back = year;
  39.                                              day_back = day;
  40.                                              month_back = month;
  41.                                              }
  42.                                              
  43.                                              /* if the months are equal */
  44.                                              if (month == month_back) {
  45.                                                        
  46.                                                        /* check if the day is before the previous day
  47.                                                        if so then set the back ups to the new date*/
  48.                                                        if (day < day_back) {
  49.                                                                year_back = year;
  50.                                                                day_back = day;
  51.                                                                month_back = month;
  52.                                                                }
  53.                                                                
  54.                                                                /* or leave the back ups in place if the criteria are not met*/
  55.                                    }
  56.                                                            }
  57.                                                                        }
  58.                                            
  59.                      }
  60.                      
  61.                      /* if the loop is finished (0/0/0 is entered) then print the earliest date (whatever is stored in the back up variables */
  62.                      printf("\nEarliest: %2d/%2d/%2d", month_back, day_back, year_back);                        
  63.                                                        
  64.    return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement