Nathan
Nathan

Reputation: 21

Trying to make it in c# where the user enters their age and it tells them what year they were born in

Heres the code I have so far and I'm stuck at the moment.

    static void Main(string[] args)
    {
        int age = 0;
        DateTime now = DateTime.Today;

        Console.WriteLine("What is your age?");
        age = Convert.ToInt32(Console.ReadLine());

    }

Upvotes: 1

Views: 387

Answers (1)

Tristan Brulotte
Tristan Brulotte

Reputation: 23

An easy way to do this would to create a string variable that is a console.readline, convert the string to and int(age), and subtract age from the current year, the result of this equation will be the year they were born. However this is still not perfect as if you have not had your birthday yet it could be a year off.

String stringAge = Console.ReadLine();
int age = Convert.ToInt32(stringAge);
//year is 2021
int result = age-2021;

Upvotes: 1

Related Questions