Advertisement
dimipan80

Calculate Age after 10 Years

Oct 17th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. /*
  2.  *  Write a program that reads your birthday from the console as text and
  3.  *  prints how old you are now and how old you will be after 10 years.
  4.  *  Input: 12.04.1991
  5.  *  Output: Now: 24
  6.  *      After 10 years: 34
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <time.h>
  11.  
  12. int main()
  13. {
  14.     int birthDay, birthMonth, birthYear;
  15.     printf("Enter your Birth Date exactly in this format [DD.MM.YYYY]: ");
  16.     if (scanf("%d.%d.%d", &birthDay, &birthMonth, &birthYear) == 1)
  17.     {
  18.         printf("Error! - Invalid input!\n");
  19.         return 1;
  20.     }
  21.    
  22.     time_t today = time(NULL);
  23.     struct tm * timeNow = localtime(&today);
  24.    
  25.     int currentYear = timeNow -> tm_year + 1900;
  26.     int currentMonth = timeNow -> tm_mon + 1;
  27.     int currentDay = timeNow -> tm_mday;
  28.    
  29.     int age = currentYear - birthYear;
  30.    
  31.     if (birthMonth < currentMonth)
  32.     {
  33.         printf("Now: %d\nAfter 10 years: %d\n", age, age + 10);
  34.     }
  35.     else if (birthMonth == currentMonth)
  36.     {
  37.         if (birthDay <= currentDay)
  38.         {
  39.             printf("Now: %d\nAfter 10 years: %d\n", age, age + 10);        
  40.         }
  41.         else
  42.         {
  43.             printf("Now: %d\nAfter 10 years: %d\n", age - 1, age + 9);
  44.         }
  45.     }
  46.     else
  47.     {
  48.         printf("Now: %d\nAfter 10 years: %d\n", age - 1, age + 9);
  49.     }
  50.        
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement