Guest User

SoftUni Programming Basics Age After X Years

a guest
Oct 26th, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. using System;
  2.  
  3. class AgeAfterXYears
  4. {
  5. static void Main()
  6. {
  7. string inputDate;
  8.  
  9. Console.Write("Please write in your date in this manner (e.g. 19.04.1991): ");
  10.  
  11.  
  12. inputDate = Console.ReadLine();
  13.  
  14. DateTime dt;
  15.  
  16. try
  17. {
  18. dt = Convert.ToDateTime(inputDate);
  19.  
  20. }
  21.  
  22. catch
  23. {
  24. var temp = "1.1.1900";
  25. dt = Convert.ToDateTime(temp);
  26. Console.WriteLine("\nWrong date!");
  27. }
  28.  
  29. var theBd = new DateTime(dt.Year, dt.Month, dt.Day);
  30.  
  31. Console.WriteLine("\nAssuming you are born: " + dt.Month + "." + dt.Day+"."+dt.Year);
  32.  
  33. var nowYears = (DateTime.Now - theBd).TotalDays / 365.2425;
  34.  
  35. var incrYears = 10; //you could change if you'd like to say in how many years
  36.  
  37. var afterXYears = incrYears + nowYears;
  38.  
  39. if (nowYears < 0)
  40. {
  41. Console.WriteLine("\nNow: You are not born yet, please wait " + nowYears * (-1) + " years.");
  42. }
  43.  
  44.  
  45. else
  46. {
  47. Console.WriteLine("\nNow: You are exactly " + nowYears + " years old!");
  48. }
  49.  
  50.  
  51. if (afterXYears < 0)
  52. {
  53. Console.WriteLine("\nAfter " + incrYears + " years you will still not be born! Please wait another " + afterXYears * (-1));
  54. }
  55.  
  56. else
  57. {
  58. Console.WriteLine("\nAfter " + incrYears + " years you would be exactly " + afterXYears + " years old!");
  59. }
  60.  
  61.  
  62.  
  63. //exit from the console
  64. Console.WriteLine("\nPlease press any key to continue");
  65. Console.ReadKey();
  66. Console.Clear();
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment