kyamaliev

Birthdays

Sep 19th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2.  
  3. class AgeAfter10Y
  4. {
  5.     static void Main()
  6.     {
  7.         string input;
  8.         int ageYears, ageMonths, futureAge, monthsDiff, daysDiff;
  9.         ageMonths = 0;
  10.         DateTime currentYear, bdate;
  11.         currentYear = DateTime.Now;
  12.         Console.WriteLine("This program will precisely calculate your age in years after 10 years");
  13.         Console.Write("Please enter your birthday in the format (dd-mm-yyyy): ");
  14.         input = Console.ReadLine();
  15.         bdate = Convert.ToDateTime(input);
  16.         monthsDiff = currentYear.Month - bdate.Month;
  17.         daysDiff = currentYear.Day - bdate.Day;
  18.         ageYears = currentYear.Year - bdate.Year;
  19.         //The next code will check if you've had your birthday this year or not and substract 1y from your age if you haven't.
  20.         if (bdate.DayOfYear == DateTime.Now.DayOfYear)
  21.         {
  22.             Console.WriteLine("Happy Birthday!");
  23.         }
  24.         else if ((monthsDiff == 0 && daysDiff < 0) || (monthsDiff < 0))
  25.         {
  26.             ageYears = ageYears - 1;
  27.             ageMonths = monthsDiff + 12;
  28.         }
  29.         else
  30.         {
  31.             ageYears = ageYears + 1;
  32.         }
  33.         futureAge = ageYears + 10;
  34.         Console.WriteLine("Your age is now is {0} years and {1} months", ageYears, ageMonths);
  35.         Console.WriteLine("Your age in 10 years will be {0} years and {1} months", futureAge, ageMonths);
  36.         Console.WriteLine("Have a nice day!");
  37.      
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment