Write a program that takes your year of birth and then calculates the age
- User
Write a program that takes your year of birth and then calculates the age
- Assistant
Here's a Python program that takes the user's year of birth and calculates their age:
import datetime # Get the current date current_date = datetime.datetime.now() # Get the year of birth from the user year_of_birth = int(input("Enter your year of birth: ")) # Calculate the age age = current_date.year - year_of_birth # Print the age print("Your age is:", age)This program uses the
datetimemodule in Python to get the current date. It asks the user to enter their year of birth and then subtracts it from the current year to calculate their age. The age is then printed on the console. - User
Write a program that takes your year of birth and then calculates the age in c++
- Assistant
Here's a C++ program that takes the user's year of birth and calculates their age:
#include <iostream> #include <ctime> int main() { // Get the current year time_t current_time = time(nullptr); tm* current_date = localtime(¤t_time); int current_year = current_date->tm_year + 1900; // Get the year of birth from the user int year_of_birth; std::cout << "Enter your year of birth: "; std::cin >> year_of_birth; // Calculate the age int age = current_year - year_of_birth; // Print the age std::cout << "Your age is: " << age << std::endl; return 0; }This program uses the
<iostream>and<ctime>libraries in C++ to get the current year and interact with the user. It asks the user to enter their year of birth and calculates their age by subtracting it from the current year. The age is then printed on the console usingstd::cout.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.