Tutorialspoint
Problem
Solution
Submissions

Convert Kilometers to Miles

Certification: Basic Level Accuracy: 85.2% Submissions: 250 Points: 5

Write a Python program that converts a distance in kilometers to miles.

Example 1
  • Input: 5
  • Output: 3.106855
  • Explanation:
    • Step 1: Apply the conversion formula: miles = kilometers × 0.621371.
    • Step 2: miles = 5 × 0.621371 = 3.106855.
    • Step 3: Return 3.106855 as the result.
Example 2
  • Input: 8.5
  • Output: 5.281654
  • Explanation:
    • Step 1: Apply the conversion formula: miles = kilometers × 0.621371.
    • Step 2: miles = 8.5 × 0.621371 = 5.281654.
    • Step 3: Return 5.281654 as the result (rounded to 6 decimal places).
Constraints
  • 0 ≤ kilometers ≤ 10^6
  • Use the conversion factor: 1 kilometer = 0.621371 miles
  • Round the result to 6 decimal places
  • Time Complexity: O(1)
  • Space Complexity: O(1)
NumberPwCSnowflake
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use the formula: miles = kilometers * 0.621371
  • Create a conversion function: def km_to_miles(km): return km * 0.621371
  • Use round(): miles = round(kilometers * 0.621371, 6)
  • Use f-string formatting: f"{kilometers * 0.621371:.6f}"

The following are the steps to convert kilometers to miles:

  • Define a function `km_to_miles(km)`.
  • Multiply the kilometers by `0.621371` to convert to miles.
  • Return the converted value.
  • Call `km_to_miles(5)` and store the result.
  • Print the miles.

Submitted Code :