Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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?

Test your Programming skills with w3resource's quiz.