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++.

  1. Using Namespace Standard: The using namespace std; statement allows us to use elements of the std namespace (e.g., cout, cin) without the need for prepending std:: to each element.

  2. Main Function: The int main() function is the entry point of the program.

  3. Variable Declaration: Two double variables kilometersPerHour and milesPerHour are declared to store the speed in kilometers per hour and miles per hour, respectively.

  4. Input: The user is prompted to enter the speed in kilometers per hour using cout and the input is stored in the kilometersPerHour variable using cin.

  5. 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.

  6. Output: The speed in miles per hour is displayed to the user using cout.

  7. Return Statement: The return 0; statement is used to indicate successful program execution and to terminate the main function.