Friday 13 May 2016

C# Coding

calculate someone's age in C#
Introduction:

We have seen so many c#.net basic examples. In this tutorial i am explaining about how to calculate age using c#.net. This is one of the best C#.net Interview Questions and Answers.
Program:

class Program
    {
        static void Main(string[] args)
        {
            DateTime birthday = new DateTime(1986, 8, 28);
            DateTime today = DateTime.Today;
            int totage = today.Year - birthday.Year;

            if (birthday > today.AddYears(-totage))
                totage--;
            Console.WriteLine("age:" + totage);
            Console.ReadLine();
        }
    }

Output: 

age:29


No comments:

Post a Comment