Advertisement
Guest User

Age In 10 Years

a guest
Mar 29th, 2015
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. // Problem 15.* Age after 10 Years -
  2. // Write a program to read your birthday from the console and print how old you are now and how old you will be after 10 years.
  3.  
  4. using System;
  5. using System.Globalization; // allows the use of (contains) the CultureInfo class
  6. using System.Threading; // allows the use of (contains) the Thread class
  7.  
  8. class AgeAfterTenYears
  9. {
  10.     static void Main()
  11.     {
  12.         /* the 2 lines of code below override the local date format settings of the particular computer,
  13.          * and ensure that the computer is able to recognize your chosen date format as a valid date.
  14.          * without the date formate instruction below,
  15.          * a computer with local MM/dd/yyyy date format settings will not recognize the input 13/02/1985 as a valid date input */
  16.  
  17.         CultureInfo bg = new CultureInfo("bg-BG");
  18.         Thread.CurrentThread.CurrentCulture = bg;
  19.  
  20.         string intro = @"Please, type below the date of your birthday
  21. in any Day Month Year format of your choice
  22. (including for instance: 1 януари 1985),
  23. and press Enter.
  24. Please note that a valid year input
  25. should be between 1850 and 2010: ";
  26.  
  27.     // input:
  28.         // if we want to use these two variables in the While statement, we should declare them outside the Do loop
  29.         string input;
  30.         DateTime birthday;
  31.  
  32.         // the Do While loop below validates the date input; if:
  33.         /* the input does not correspond to any valid D/M/Y date format;
  34.          * or the input for month is not a number between 1 and 12, neither a valid month name;
  35.          * or the input for days exceeds the maximum days number for the specific month;
  36.          * (the TryParse() method alone takes care of the 3 requirements above)
  37.          * or the year input is not between 1850 and 2010;
  38.          * in all above cases the user gets re-invited to enter a valid date
  39.          */
  40.         do
  41.         {
  42.             Console.Write(intro);
  43.             input = Console.ReadLine();
  44.  
  45.         } while (!DateTime.TryParse(input, out birthday) || birthday.Year < 1850 || birthday.Year > 2010);
  46.  
  47.     // logic:
  48.         // DateTime.Year Property gets only the year component of the respective date
  49.         int yourAgeNow = DateTime.Now.Year - birthday.Year;
  50.  
  51.         /* the code below subtracts the user's age from his birthday date, the result is a date in the year of user's birth, and if this day is before his birthday,
  52.          * subracts an year from the aboove calculated user's age
  53.          * (shortly, this year the user will get for instance 30, but his birthday hasn't yet come, so he is still 29.)*/
  54.         if (birthday > DateTime.Now.AddYears(-yourAgeNow)) yourAgeNow--;
  55.  
  56.     // printing:
  57.         Console.WriteLine("\nYou are now {0} y.o.", yourAgeNow);
  58.         Console.WriteLine("\nIn 10 years you will be {0} y.o.\n", yourAgeNow + 10);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement