Skip to content
Exercises · Q9

Q.Write a program that asks the user to enter one's name and age. Print out a message addressed to the user that tells the user the year in which he/she will turn 100 years old.

Chandigarh CbseNCERTSubjective· 3mImportance★★★★★est
88% · 22/25 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Read the user's name and age, calculate the year they turn 100 by adding (100 − age) to the current year, and print a personalized message.

Why this approach works

The problem requires three pieces of information: the user's name (a string), their current age (an integer), and the current year. Python's input() function reads strings from the console, so we need int() to convert the age into a number we can calculate with. The year someone turns 100 is straightforward arithmetic: if you are currently n years old, you will turn 100 in (100 - n) years from now, which is \text{current year} + (100 - n).

We can get the current year using Python's datetime module, which provides datetime.now().year. This avoids hardcoding the year and makes the program work correctly no matter when it is run.

Complete solution

from datetime import datetime

# Get user input
name = input("Enter your name: ")
age = int(input("Enter your age: "))

# Calculate the year they turn 100
current_year = datetime.now().year
years_to_100 = 100 - age
year_turn_100 = current_year + years_to_100

# Print the personalized message
print(f"{name}, you will turn 100 years old in the year {year_turn_100}.")

Expected output

If the user enters "Arjun" and 18 in the year 2025:

Enter your name: Arjun
Enter your age: 18
Arjun, you will turn 100 years old in the year 2107.

Key lines explained

age = int(input("Enter your age: "))

The input() function always returns a string. To perform arithmetic, we must convert it to an integer with int(). Forgetting this conversion would cause a TypeError when we try to subtract.

years_to_100 = 100 - age

If someone is 18, they need 100 - 18 = 82 more years to reach 100. This is the offset we add to the current year.

datetime.now().year …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.