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.
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
- C++ Program Convert Distance Between Cities to Meters, Feet, Inches, and Centimeters
- Operator Overloading in a 3D Vector Class in C++
- Counting Characters in a String in C++
- Explaining the getline Function in C++
- Data Hiding in C++
- Efficient Array Sorting in C++: Static Overloaded Functions for Floats and Integers
- Programm to demonstrate constructor with default arguments
- Understanding Function Call by Value in C++
- Exploring Virtual Functions with Default Arguments in C++
- Multiple Inheritance in C++
- Binary + Operator Overloading In CPP
- Exploring Class-to-Basic Type Conversion with a Product Example in C++
- Creating a Diamond Pattern in the CPP Program
- Write a CPP program to perform arithmetic operations using an inline function.
- Write a program in C++ to create a structure named complex having data members real and images. Create member function add_complex which takes structure as an argument and returns structure. Using the function add two complex numbers.
- Write a program to generate the following pattern