C++ Program Convert Distance Between Cities to Meters, Feet, Inches, and Centimeters

Introduction

This code is a C++ program that converts a distance given in kilometers to meters, centimeters, feet, and inches. It prompts the user to enter the distance in kilometers and then performs the necessary calculations to convert it to the desired units.

CPP

Key Concepts

  • Variables: The program declares a variable distanceKm to store the input distance in kilometers. It also declares variables distanceMeter, distanceCm, distanceFeet, and distanceInch to store the converted distances in meters, centimeters, feet, and inches, respectively.
  • Input and Output: The program uses the cin object to read the input distance from the user and the cout object to display the converted distances.
  • Constants: The program defines conversion factors as constants, such as kmToMeter, meterToCm, meterToFeet, and feetToInch, to perform the necessary calculations.

Code Structure

The code follows a simple structure:
  • It includes the necessary header file iostream for input and output operations.
  • The main() function is the entry point of the program.
  • It declares the variable distanceKm to store the input distance in kilometers.
  • It prompts the user to enter the distance in kilometers using the cout object and reads the input using the cin object.
  • It defines the conversion factors as constants.
  • It performs the necessary calculations to convert the distance from kilometers to meters, centimeters, feet, and inches.
  • It outputs the converted distances using the cout object.
  • It returns 0 to indicate successful program execution.

Code Examples

Here's an example of how the program works:
C++
#include <iostream>
using namespace std;

int main() {
    // Declare variables
    double distanceKm;

    // Input distance in kilometers
    cout << "Enter the distance between two cities (in kilometers): ";
    cin >> distanceKm;

    // Conversion factors
    const double kmToMeter = 1000.0;
    const double meterToCm = 100.0;
    const double meterToFeet = 3.33;
    const double feetToInch = 12.0;

    // Convert distance
    double distanceMeter = distanceKm * kmToMeter;
    double distanceCm = distanceMeter * meterToCm;
    double distanceFeet = distanceMeter * meterToFeet;
    double distanceInch = distanceFeet * feetToInch;

    // Output the converted distances
    cout << "Distance in meters: " << distanceMeter << " meters" << endl;
    cout << "Distance in centimeters: " << distanceCm << " centimeters" << endl;
    cout << "Distance in feet: " << distanceFeet << " feet" << endl;
    cout << "Distance in inches: " << distanceInch << " inches" << endl;

    return 0;
}

Conclusion

This C++ program demonstrates how to convert a distance given in kilometers to meters, centimeters, feet, and inches. It prompts the user for input, performs the necessary calculations using conversion factors, and outputs the converted distances. This program can be used as a starting point for more complex distance conversion applications.

Other C++ Program

Next Post Previous Post
No Comment
Add Comment
comment url