Getting century from year
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting a year into a century looks trivial until you hit boundary values such as 100, 1900, or 2000. The mistake most solutions make is forgetting that centuries start at year 1, not year 0, so exact multiples of one hundred belong to the century you just finished, not the next one.
The Correct Formula
For positive integer years, the standard formula is:
(year - 1) // 100 + 1
The subtraction by one is the key. It shifts the upper boundary of each century into the correct integer-division bucket.
Output:
This matches the historical convention: years 1 through 100 are the 1st century, years 101 through 200 are the 2nd century, and so on.
Why Plain Division Fails
A naive implementation often looks like this:
That fails on most non-boundary years. For example:
- '
1900 // 100gives19, which happens to be correct' - '
1901 // 100also gives19, which is wrong'
So the bug is not obvious if you only test round century values. That is why good test cases should include both the edge and the year immediately after it.
Another Equivalent Form
Some developers prefer a remainder-based approach:
This is logically fine, but the shifted formula is shorter and usually easier to reason about once you understand why it works.
JavaScript Version
The same idea translates directly to JavaScript:
The important part is still the shift by one before division.
Testing the Right Cases
Boundary cases are where century logic proves itself. Good tests include:
- '
1should return1' - '
100should return1' - '
101should return2' - '
2000should return20' - '
2001should return21'
If your function passes those, the rest of the range is usually correct as well.
Think About Domain Rules
Most programming exercises assume the input is a positive Gregorian-style year number. If you need to handle year zero, BCE dates, or historical calendar transitions, the question is no longer pure arithmetic; it becomes a domain modeling problem.
For example, astronomical year numbering uses a year zero, while many historical conventions do not. A generic helper function should document which interpretation it supports instead of pretending the distinction does not exist.
Common Pitfalls
The biggest pitfall is using year // 100 directly and believing it works because it passed one or two simple tests. It fails immediately after each century boundary.
Another common issue is not validating the input. If your function is only defined for positive years, reject zero and negative values explicitly.
Developers also sometimes mix the arithmetic with string formatting too early, returning labels such as 21st century from the same function. Keeping the numeric computation separate from display formatting makes testing easier and avoids coupling business logic to presentation rules.
Summary
- For positive years, the standard formula is
(year - 1) // 100 + 1. - Exact multiples of one hundred are the edge cases that expose incorrect solutions.
- Testing
100,101,2000, and2001is a quick way to validate the logic. - Keep numeric century calculation separate from display formatting.
- If your application needs BCE or year-zero rules, define that behavior explicitly rather than assuming the simple formula covers it.
Related reading
- Getting median out of frequency table counting sort
- Getting parent of a vertex in a perfect binary tree
- Getting the closest string match
- Getting the lowest possible sum from numbers' difference
- Getting the submatrix with maximum sum?
- Git Confusion about merge algorithm, conflict format, and interplay with mergetools
- Given 2 sorted arrays of integers, find the nth largest number in sublinear time
- Given a 1 TB data set on disk with around 1 KB per data record, how can I find duplicates using 512 MB RAM and infinite disk space?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.