Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Operators and Expressions

Convert minutes to hours and minutes - Python Program

Example 1:

total_minutes = int(input("Enter minutes: ")) hours = total_minutes // 60 minutes = total_minutes % 60 print(f"{total_minutes} minutes = {hours} hour(s) and {minutes} minute(s)")

Output

 
OUTPUT  :
Enter minutes: 135
135 minutes = 2 hour(s) and 15 minute(s)
 

// for floor division, % for remainder.

Example 2: Advanced Program

def convert_minutes_to_hours_minutes(total_minutes): """ Converts a total number of minutes into hours and remaining minutes. Args: total_minutes (int): The total number of minutes to convert. Returns: tuple: A tuple containing two integers: (hours, remaining_minutes). """ hours = total_minutes // 60 # Integer division to get full hours remaining_minutes = total_minutes % 60 # Modulo operator to get remaining minutes return hours, remaining_minutes # Get input from the user try: minutes_input = int(input("Enter the total number of minutes: ")) if minutes_input < 0: print("Please enter a non-negative number of minutes.") else: # Perform the conversion hours_result, minutes_result = convert_minutes_to_hours_minutes(minutes_input) # Display the result print(f"{minutes_input} minutes is equal to {hours_result} hours and {minutes_result} minutes.") except ValueError: print("Invalid input. Please enter an integer.")

Output

 
OUTPUT  :
Enter the total number of minutes: 245
245 minutes is equal to 4 hours and 5 minutes.