Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Determine Century

Convert a positive year into its century with Python integer arithmetic. Students handle exact hundreds separately and build the correct English ordinal suffix.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 9 of 10
determine-century.py
1# Take year
2year = int(input("Enter a year: "))
3
4# Calculate century
5if year % 100 == 0:
6 century = year // 100
7else:
8 century = (year // 100) + 1
9
10# Format century
11if 11 <= century % 100 <= 13:
12 suffix = "th"
13elif century % 10 == 1:
14 suffix = "st"
15elif century % 10 == 2:
16 suffix = "nd"
17elif century % 10 == 3:
18 suffix = "rd"
19else:
20 suffix = "th"
21
22print(f"{century}{suffix} century")

Output

Enter a year: 2024
21st century

Enter a year: 1900
19th century

Enter a year: 1950
20th century

In this one

  • Handle exact-century boundaries
  • Use integer division
  • Construct ordinal suffixes

How it works

  1. 1

    Divide the year into hundreds with the boundary check.

  2. 2

    Choose th for eleven-to-thirteen endings, otherwise the final digit.

  3. 3

    Print the century number with its suffix.

Edge cases

What century contains 1900?

What century contains 1900?

The nineteenth; it is the final year of that century.

Does year zero fit the lesson?

Does year zero fit the lesson?

No. This calculation assumes positive numbered years.

What's going on

A century ends at a multiple of one hundred. Thus 1900 belongs to the nineteenth century, while 1901 begins the twentieth. The division branch keeps that boundary from moving a century too far.

For a nonmultiple, integer division gives completed hundreds and adding one gives the current century. The suffix follows the century's final digits, with eleven through thirteen as exceptions. Year 2024 therefore produces 21st century.

What century contains 1900? The nineteenth; it is the final year of that century.

Does year zero fit the lesson? No. This calculation assumes positive numbered years.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which century contains 2001?

Pick an answer to check it.

Question 2

What century contains 1900?

Pick an answer to check it.

Questions people ask

What century contains 1900?

The nineteenth; it is the final year of that century.

Does year zero fit the lesson?

No. This calculation assumes positive numbered years.

Which century contains 2001?

21st.