C++ Kilometers Per Hour to Miles Per Hour Conversion
C++ Kilometers Per Hour to Miles Per Hour Conversion
#include <iostream>
using namespace std;
int main() {
double kilometersPerHour, milesPerHour;
cout << "Enter speed in kilometers per hour: ";
cin >> kilometersPerHour;
milesPerHour = kilometersPerHour * 0.621371;
cout << "Speed in miles per hour: " << milesPerHour;
return 0;
}
Explanation:
1. Include Input/Output Stream Library: The #include <iostream>
directive is used to include the Input/Output stream library, which allows us to perform input and output operations in C++.
Using Namespace Standard: The
using namespace std;
statement allows us to use elements of thestd
namespace (e.g., cout, cin) without the need for prependingstd::
to each element.Main Function: The
int main()
function is the entry point of the program.Variable Declaration: Two double variables
kilometersPerHour
andmilesPerHour
are declared to store the speed in kilometers per hour and miles per hour, respectively.Input: The user is prompted to enter the speed in kilometers per hour using
cout
and the input is stored in thekilometersPerHour
variable usingcin
.Conversion: The speed in kilometers per hour is converted to miles per hour using the conversion factor 0.621371 and stored in the
milesPerHour
variable.Output: The speed in miles per hour is displayed to the user using
cout
.Return Statement: The
return 0;
statement is used to indicate successful program execution and to terminate themain
function.