Age Calculation C# Cognizant Handson Solution
Famous Insurance company has a requirement to calculate the age of their customer based on the date of birth received as a string from the user.
Write a program that receives the customer date of birth in the format (dd-mm-yyyy).
Pass this value to a method, ‘calculateAge’ which returns the calculated age. Keep the method ‘static’.
Method to implement:
public static int calculateAge(string dateOfBirth)
Sample Input :
Enter the date of birth (dd-mm-yyyy): 22-10-1984
Sample Output:
36
Program.cs
using System.Linq; using System; namespace DateEx1 { public class Program { public static void Main(string[] args) { Console.WriteLine("Enter the date of birth (dd-mm-yyyy): "); var dobText = Console.ReadLine(); var age = calculateAge(dobText); Console.WriteLine(age); } public static int calculateAge(string dateOfBirth) { var timespan = DateTime.Now - DateTime.Parse(String.Join("-", dateOfBirth.Split("-").Reverse())); return timespan.Days / 365; } } }