Q:

Write a C# program to get the century from a year

0

Write a C# program to get the century from a year

Sample Output:

True
True
False
True
True
False
True

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
public class Exercise
{
     public static int centuryFromYear(int year)
        {
            return (int)(year / 100) + ((year % 100 == 0) ? 0 : 1);
        }

        public static void Main()
        {
            Console.WriteLine(centuryFromYear(1799) == 18);
            Console.WriteLine(centuryFromYear(1900) == 19);
            Console.WriteLine(centuryFromYear(1901) == 19);
            Console.WriteLine(centuryFromYear(1901) == 20);
            Console.WriteLine(centuryFromYear(1806) == 19);
            Console.WriteLine(centuryFromYear(1568) == 20);
            Console.WriteLine(centuryFromYear(2010) == 21);
        }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now