Kata 4 solution


You need to change the type of monthsToAdd to be a double:

double monthsToAdd = Convert.ToInt32(Console.ReadLine());

Note that Convert.ToInt32 produces an int. However, C# is able to figure out that the int value produced is being put into a variable of type double, so it automatically converts the int into a double for us. This means the division that occurs in:

double yearsInMonths = monthsToAdd / 12;

Now works as expected, and you get fractional numbers in the output.


Convert from years to months by multiplying age by 12. Then convert from years to days by multiplying by 365:

int months = age * 12;
int days = age * 365;

You can then output this using Console.WriteLine For instance:

Console.WriteLine(age + " years, or " + months " months, or " + days " days old.")
Copyright Mikiel Agutu 2019