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.
1# Take year2year = int(input("Enter a year: "))34# Calculate century5if year % 100 == 0:6 century = year // 1007else:8 century = (year // 100) + 1910# Format century11if 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"2122print(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
Divide the year into hundreds with the boundary check.
- 2
Choose th for eleven-to-thirteen endings, otherwise the final digit.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum