Advertisement
Masovski

15. Age after 10 years

Mar 8th, 2014
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. //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.
  2.  
  3. using System;
  4.  
  5. class AgeAfter10Years
  6. {
  7.     static void Main()
  8.     {
  9.         DateTime birthDay; // Declaring variable 'birthDay' of type 'DateTime'
  10.         if (DateTime.TryParse(Console.ReadLine(), out birthDay)) // Getting birthdate and initializing it to 'birthDay'
  11.         {
  12.             DateTime today = DateTime.Today; // Getting the current date
  13.             int age = today.Year - birthDay.Year; //Getting the age
  14.             if (birthDay > today.AddYears(-age)) age--; // Checking for accuracy of the age
  15.             Console.WriteLine(age); // Displaying the current age
  16.             Console.WriteLine(age + 10); // Displaying the age after 10 years
  17.         }  
  18.         else
  19.         {
  20.             Console.WriteLine("Please enter the birthday in proper format (e.g. dd.mm.yyyy)");
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement