w3resource

C# Sharp Exercises: Find the century from a given year

C# Sharp String: Exercise-65 with Solution

From Wikipedia –
A century is a period of 100 years. Centuries are numbered ordinally in English and many other languages. The word century comes from the Latin centum, meaning one hundred. Century is sometimes abbreviated as c.
Although a century can mean any arbitrary period of 100 years, there are two viewpoints on the nature of standard centuries. One is based on strict construction, while the other is based on popular perception.
According to the strict construction, the 1st century AD began with AD 1 and ended with AD 100, the 2nd century spanning the years 101 to 200, with the same pattern continuing onward. In this model, the n-th century starts with the year that ends with "01", and ends with the year that ends with "00"; for example, the 20th century comprises the years 1901 to 2000 in strict usage.
In popular perception and practice, centuries are structured by grouping years based on sharing the 'hundreds' digit(s). In this model, the n-th century starts with the year that ends in "00" and ends with the year ending in "99"; for example, the years 1900 to 1999, in popular culture, constitute the 20th century.
Write a C# Sharp program to find the century from a given year.

Sample Data:
(1435) -> "15th century"
(1567) -> "16th century"
(1921) -> "20th century"
(2014) -> "21st century"

Sample Solution:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
   class Program
   {
       static void Main(string[] args)
       {
           int n = 1435;
           Console.WriteLine("Original year: " + n);
           Console.WriteLine("Century of the said year: " + test(n));
           n = 1567;
           Console.WriteLine("\nOriginal year: " + n);
           Console.WriteLine("Century of the said year: " + test(n));
           n = 1921;
           Console.WriteLine("\nOriginal year: " + n);
           Console.WriteLine("Century of the said year: " + test(n));
           n = 2014;
           Console.WriteLine("\nOriginal year: " + n);
           Console.WriteLine("Century of the said year: " + test(n));
       }
       public static string test(int year)
       {
           int n = year % 100 == 0 ? year / 100 : year / 100 + 1;
           if (n > 20)
               return n + "st century";
           else
               return n + "th century";
       }
   }
}

Sample Output:

Original year: 1435
Century of the said year: 15th century

Original year: 1567
Century of the said year: 16th century

Original year: 1921
Century of the said year: 20th century

Original year: 2014
Century of the said year: 21st century

Flowchart :

Flowchart: C# Sharp Exercises - Find the century from a given year.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Longest abecedarian word in a array of words.
Next C# Sharp Exercise: Highest frequency character(s) in the words of a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter