Advertisement
darighteous1

AgeIn10Years

Oct 27th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2.  
  3. class AgeIn10Years
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Please, enter your date of birth");
  8.         DateTime date = DateTime.Parse(Console.ReadLine()); // this will initialize a variable "date", that will hold the value of our input
  9.         DateTime dateNow = DateTime.Now; // the variable dateNow holds the value of the current date
  10.         TimeSpan age = dateNow - date; // This will substract the input date (your birth date) from the current date and returns a result in total days
  11.         int ageInYears = age.Days / 365; // we divide the total days by 365 to get your age in years
  12.  
  13.  
  14.         //now we can just print our age after 10 years by simply
  15.         // Console.WriteLine(ageInYears + 10);
  16.        
  17.         //Or, we can do it a bit more elegant
  18.         if (ageInYears < 1) //this checks if you are younger than 1 year and returns your age in months
  19.         {
  20.             int ageInMonths = age.Days / 12;
  21.             Console.WriteLine("Today, you're approximately {0} months old.", ageInMonths);
  22.             Console.WriteLine("After 10 years you'll be 10 years and {0} months old.", ageInMonths);
  23.         }
  24.         else // if you're 1 year old or older, the program returns your age in ten years
  25.         {
  26.             Console.WriteLine("Today, you're {0} years old.", ageInYears);
  27.             Console.WriteLine("After 10 years you'll be {0} years old.", ageInYears + 10);
  28.         }
  29.  
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement