-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathconvert_kms_to_miles.cpp
45 lines (36 loc) · 1.36 KB
/
convert_kms_to_miles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*******************************************************************************
*
* Program: Convert Kilometers To Miles
*
* Description: Program to convert a distance in kilometers to a distance in
* miles using C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=2GdvqM-j-C0
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Variables to store distances in miles and kilometers. Type double is
// used because distances may have decimal places and double type variables
// can store numbers with decimal places.
double miles, kilometers;
// Prompt the user to enter the distance in kilometers
cout << "Kilometers: ";
// Store the entered distance in the variable kilometers
cin >> kilometers;
// Convert the distance in kilometers to the distance in miles and store
// the result in the variable miles using the formula found here:
// https://en.wikipedia.org/wiki/Kilometre
miles = 0.621371 * kilometers;
// Output the text "Miles: "
cout << "Miles: ";
// Then output the value stored in the variable miles with 2 decimal digits
// of precision using the stream manipulators fixed and setprecision.
cout << fixed << setprecision(2) << miles << endl;
return 0;
}