Question 19 of 22intermediate🔧 ApplyLong Answer4 marks

Write a program that asks the user to enter their name and age. Print a message addressed to the user that tells the user the year in which they will turn 100 years old.

Correct Answer

Program:

python

Program to find the year when user turns 100

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

years_remaining = 100 - age current_year = 2024 year_100 = current_year + years_remaining

print(name + ", you will turn 100 years old in the year", year_100)

Sample Output:

Enter your name: Rahul Enter your age: 17 Rahul, you will turn 100 years old in the year 2107

Explanation:

The program uses the input() function to accept the user's name as a string. For the age, the int() function is used to convert the string input to an integer, as shown in Example 5.15. This typecasting is necessary because input() always returns a string value.

The calculation determines how many years remain until the user turns 100 by subtracting the current age from 100. This value is then added to the current year (2024) to find the target year. The print() function displays the personalized message with the calculated year.

Exercise: EXERCISE | Q: 19 | (Page 32)
For More Understanding

Explanation

This question tests understanding of input/output functions and typecasting in Python. The textbook context in Example 5.14 and 5.15 demonstrates that input() returns a string, and int() is required to convert it to an integer for arithmetic operations. The print() function is used to output the result as shown in Program 5-2.

Solution Steps

  1. Step 1: Accept name using input() function

  2. Step 2: Accept age using int(input()) to convert string to integer

  3. Step 3: Calculate years remaining as 100 - age

  4. Step 4: Add years remaining to current year to get the year when user turns 100

  5. Step 5: Display the result using print() function