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

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

Next Post Previous Post
No Comment
Add Comment
comment url