w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Get the century from a year

C# Sharp Basic: Exercise-54 with Solution

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

Sample Solution:

C# Sharp Code:

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);
        }
}

Sample Output:

True
True
False
True
True
False
True 

Flowchart:

Flowchart: C# Sharp Exercises - Get the century from a year

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to check if an array contains an odd number.
Next: Write a C# program to find the pair of adjacent elements that has the largest product of an given array.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming