kilometers to miles in C++ with simple conversion formula 1 Kilometers = 0.621 miles.
For example:
Input: 10 kilometers
Output: 6.21 miles
Program will use unit formula to convert distance unit kilometers to mile.
Example, 10 kilometers = —– miles?
Since, 1 km = 0.621 miles
hence, 10 km = 10*0.621 = 6.21 miles.
C++ Program Description:
Program will prompt user to input the distance in kilometers and store the entered value in a float variable.
It will use float KilometersToMilesConversion(float kilometers)user defined method to convert kilometers to miles and return the float value in miles.
Code for kilometers to miles in C++
#include <iostream>
using namespace std;
float KilometersToMilesConversion(float kilometers) {
// 1 kilometer = 0.621
float miles = kilometers * 0.621;
return miles;
}
//Test program
int main()
{
float kilometers;
cout << "Enter the distance in kilometers:";
cin >> kilometers;
//Call conversion function
float miles = KilometersToMilesConversion(kilometers);
cout << kilometers << " is equal to "<< miles << " Miles";
return 0;
}
Output:
Enter the distance in kilometers:10
10 is equal to 6.21 Miles