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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework