none
c# program to calculate birthday RRS feed

  • Question

  • i need a program that asks a person about his birth day. If the person is an adult (15 or more), it computes his age in yeas, if the person is a child, it computes his age in days. The output should be:

    You are adult. Your age is xx years.

    You are young. Your age is xxxx days.

    Thanks a lot.

    Monday, October 8, 2018 4:59 PM

Answers

  • Write 2 methods to calculate number of years and number of days.

    From main method, get the date and call number of years method. If condition to find out if years > 15 and display message accordingly.

    static void Main(string[] args)
            {
                Console.WriteLine("Enter DOB (mm/dd/yyyy): ");
                string dob = Console.ReadLine();
                DateTime dt = DateTime.Parse(dob);
    
                if (CalculateYears(dt) > 15)
                    Console.WriteLine("You are adult. Your age is "+ CalculateYears(dt) + " years.");
                else
                    Console.WriteLine("You are young. Your age is "+ CalculateDays(dt) + " days.");
    
                Console.ReadLine();
            }
    
            public static int CalculateDays(DateTime dob)
            {
                DateTime dt = new DateTime(dob.Year, dob.Month, dob.Day);
                return DateTime.Now.Subtract(dt).Days;
            }
    
            public static int CalculateYears(DateTime dob)
            {
                return ((DateTime.Now.Year - dob.Year) * 372 + (DateTime.Now.Month - dob.Month) * 31 + (DateTime.Now.Day - dob.Day)) / 372;
            }


    Thanks, AT

    • Marked as answer by Abdullah1995 Monday, October 8, 2018 6:51 PM
    Monday, October 8, 2018 5:24 PM

All replies

  • Write 2 methods to calculate number of years and number of days.

    From main method, get the date and call number of years method. If condition to find out if years > 15 and display message accordingly.

    static void Main(string[] args)
            {
                Console.WriteLine("Enter DOB (mm/dd/yyyy): ");
                string dob = Console.ReadLine();
                DateTime dt = DateTime.Parse(dob);
    
                if (CalculateYears(dt) > 15)
                    Console.WriteLine("You are adult. Your age is "+ CalculateYears(dt) + " years.");
                else
                    Console.WriteLine("You are young. Your age is "+ CalculateDays(dt) + " days.");
    
                Console.ReadLine();
            }
    
            public static int CalculateDays(DateTime dob)
            {
                DateTime dt = new DateTime(dob.Year, dob.Month, dob.Day);
                return DateTime.Now.Subtract(dt).Days;
            }
    
            public static int CalculateYears(DateTime dob)
            {
                return ((DateTime.Now.Year - dob.Year) * 372 + (DateTime.Now.Month - dob.Month) * 31 + (DateTime.Now.Day - dob.Day)) / 372;
            }


    Thanks, AT

    • Marked as answer by Abdullah1995 Monday, October 8, 2018 6:51 PM
    Monday, October 8, 2018 5:24 PM
  • thanks brother but there is 2 error

    Monday, October 8, 2018 6:05 PM