w3resource

C++ Exercises: Converts kilometers per hour to miles per hour


Kilometers to Miles Conversion

Write a C++ program that converts kilometers per hour to miles per hour.

Visual Presentation:

C++ Exercises: Converts kilometers per hour to miles per hour

Sample Solution:

C++ Code :

#include<iostream> // Including input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    float kmph, miph; // Declaring floating-point variables for kilometers per hour and miles per hour

    cout << "\n\n Convert kilometers per hour to miles per hour :\n"; // Displaying the purpose of the program
    cout << "----------------------------------------------------\n";

    cout << " Input the distance in kilometer : "; // Prompting the user to input distance in kilometers
    cin >> kmph; // Taking input of distance in kilometers from the user

    miph = (kmph * 0.6213712); // Converting kilometers per hour to miles per hour using the conversion factor
    // Displaying the converted values
    cout << " The " << kmph << " Km./hr. means " << miph << " Miles/hr." << endl;

    cout << endl; // Displaying an empty line for better readability

    return 0; // Returning 0 to indicate successful program execution
}

Sample Output:

 Convert kilometers per hour to miles per hour :                       
----------------------------------------------------                   
 Input the distance in kilometer : 5                                   
 The 5 Km./hr. means 3.10686 Miles/hr.

Flowchart:

Flowchart: Converts kilometers per hour to miles per hour

For more Practice: Solve these Related Problems:

  • Write a C++ program that converts kilometers per hour to miles per hour using user-defined functions and error-checks negative inputs.
  • Write a C++ program to convert a list of speeds from kilometers per hour to miles per hour, formatting the output in a right-aligned table.
  • Write a C++ program that converts kilometers per hour to miles per hour and then determines whether the speed exceeds a given threshold.
  • Write a C++ program that reads speeds in kilometers per hour from a file, converts each to miles per hour, and displays the results with proper formatting.

Go to:


PREV : Centimeters to Meters and Kilometers.
NEXT : Third Angle of Triangle.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.