This Python program defines a Distance class that represents a distance using kilometers, meters, and centimeters. The class provides methods to add two distances and to display a distance in a human-readable format. Here's an explanation of the program:
When you run this program, it will allow you to input two distances and then calculate and display the sum of those distances. The program takes care of adjusting units when necessary, ensuring that the result is displayed correctly in terms of kilometers, meters, and centimeters.
class Distance: def __init__(self, km=0, m=0, cm=0): self.km = km self.m = m self.cm = cm def add(self, other_distance): total_km = self.km + other_distance.km total_m = self.m + other_distance.m total_cm = self.cm + other_distance.cm # Adjust units if necessary if total_cm >= 100: total_m += total_cm // 100 total_cm %= 100 if total_m >= 1000: total_km += total_m // 1000 total_m %= 1000 return Distance(total_km, total_m, total_cm) def display(self): return f"{self.km} KM {self.m} M {self.cm} CM" print("Enter First Distance") km1 = int(input("Enter Kilometers : ")) m1 = int(input("Enter Meters : ")) cm1 = int(input("Enter Centimeters : ")) distance1 = Distance(km1, m1, cm1) print("\nEnter Second Distance") km2 = int(input("Enter Kilometers : ")) m2 = int(input("Enter Meters : ")) cm2 = int(input("Enter Centimeters : ")) distance2 = Distance(km2, m2, cm2) # Add the distances result_distance = distance1.add(distance2) # Display the result print("Sum of both Distances is :",result_distance.display())
Enter First Distance Enter Kilometers : 53 Enter Meters : 150 Enter Centimeters : 240 Enter Second Distance Enter Kilometers : 34 Enter Meters : 123 Enter Centimeters : 210 Sum of both Distances is : 87 KM 277 M 50 CM
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions