CPP Program to accept Kilometer and convert it into meter and centimeter
Write a CPP Program to accept a Kilometer and convert it into a meter and centimeter.
Introduction
This program is designed to convert a distance given in kilometers to meters and centimeters. It prompts the user to enter a distance in kilometers, performs the necessary calculations, and displays the converted distances in meters and centimeters.
Key Concepts
- Variables: The program declares three variables to store the distances in kilometers, meters, and centimeters.
- Input/Output: The program uses the cin object to read the distance in kilometers entered by the user and the cout object to display the converted distances in meters and centimeters.
- Arithmetic Operations: The program uses multiplication to convert the distance from kilometers to meters and centimeters.
Code Structure
The code follows a simple structure:
- Include the necessary header file (iostream) for input/output operations.
- Use the using namespace std; directive to avoid having to prefix standard library objects and functions with std::.
- Define the main() function, which is the entry point of the program.
- Declare the variables kilometers, meters, and centimeters to store the distances.
- Prompt the user to enter a distance in kilometers using the cout object.
- Read the distance entered by the user using the cin object and store it in the kilometers variable.
- Perform the necessary calculations to convert the distance from kilometers to meters and centimeters.
- Display the converted distances in meters and centimeters using the cout object.
- Return 0 to indicate successful program execution.
Code Examples
Here is an example of how the program works:
C++
#include <iostream> using namespace std; int main() { // Declare variables double kilometers, meters, centimeters; // Input from the user cout << "Enter distance in kilometers: "; cin >> kilometers; // Conversion meters = kilometers * 1000; // 1 kilometer = 1000 meters centimeters = meters * 100; // 1 meter = 100 centimeters // Display the result cout << "The distance in meters is: " << meters << " m" << endl; cout << "The distance in centimeters is: " << centimeters << " cm" << endl; return 0; }
In this example, the program prompts the user to enter a distance in kilometers. Let's say the user enters 5. The program then converts this distance to meters and centimeters using the conversion factors (1 kilometer = 1000 meters, 1 meter = 100 centimeters). The program then displays the converted distances as output:
Output
Enter distance in kilometers: 5 The distance in meters is: 5000 m The distance in centimeters is: 500000 cm
Conclusion
This program demonstrates how to convert a distance given in kilometers to meters and centimeters using simple arithmetic operations. By understanding the code structure and key concepts, you can modify and extend this program to suit your specific needs.
Other C++ Program
- C++ Program Convert Distance Between Cities to Meters, Feet, Inches, and Centimeters
- C++ Distance Calculation Program
- 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