Logic Building
# Take year
year = int(input("Enter a year: "))
# Calculate century
if year % 100 == 0:
century = year // 100
else:
century = (year // 100) + 1
# Format century
if century == 1:
suffix = "st"
elif century == 2:
suffix = "nd"
elif century == 3:
suffix = "rd"
else:
suffix = "th"
print(f"{century}{suffix} century")Output
Enter a year: 2024 21st century Enter a year: 1900 19th century Enter a year: 1950 20th century
Calculate century from year.
Key Concepts:
- If year divisible by 100: century = year/100
- Otherwise: century = (year/100) + 1
- Add appropriate suffix (st, nd, rd, th)