1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; int main() { double meters; int choice; double result; // Display the menu cout << "Unit Conversion: Length (Meters to other units)" << endl; cout << "1. Convert to Kilometers" << endl; cout << "2. Convert to Centimeters" << endl; cout << "3. Convert to Millimeters" << endl; cout << "Enter your choice (1-3): "; cin >> choice; // Input the length in meters cout << "Enter the length in meters: "; cin >> meters; // Perform the conversion based on user's choice switch (choice) { case 1: result = meters / 1000.0; cout << meters << " meters is " << result << " kilometers." << endl; break; case 2: result = meters * 100.0; cout << meters << " meters is " << result << " centimeters." << endl; break; case 3: result = meters * 1000.0; cout << meters << " meters is " << result << " millimeters." << endl; break; default: cout << "Invalid choice!" << endl; break; } return 0; } |
- Input and Menu Display:
- The program first displays a menu with three options for converting meters to kilometers, centimeters, or millimeters.
- The user is prompted to enter their choice.
- Switch Statement:
- The program uses a
switch
statement to determine which conversion to perform based on the user’s choice. - Each
case
corresponds to a different unit conversion:case 1
: Converts meters to kilometers by dividing the length in meters by 1000.case 2
: Converts meters to centimeters by multiplying the length in meters by 100.case 3
: Converts meters to millimeters by multiplying the length in meters by 1000.
- If the user enters an invalid choice, the
default
case is executed, printing an error message.
- The program uses a
- Output:
- The program calculates the result based on the conversion and prints it to the console.
Example 1: Convert Meters to Kilometers
Input:
- Choice:
1
- Meters:
1500
Output:
1234567Unit Conversion: Length (Meters to other units)1. Convert to Kilometers2. Convert to Centimeters3. Convert to MillimetersEnter your choice (1-3): 1Enter the length in meters: 15001500 meters is 1.5 kilometers.Example 2: Convert Meters to Centimeters
Input:
- Choice:
2
- Meters:
2.5
Output:
1234567Unit Conversion: Length (Meters to other units)1. Convert to Kilometers2. Convert to Centimeters3. Convert to MillimetersEnter your choice (1-3): 2Enter the length in meters: 2.52.5 meters is 250 centimeters.Example 3: Convert Meters to Millimeters
Input:
- Choice:
3
- Meters:
0.75
Output:
1234567Unit Conversion: Length (Meters to other units)1. Convert to Kilometers2. Convert to Centimeters3. Convert to MillimetersEnter your choice (1-3): 3Enter the length in meters: 0.750.75 meters is 750 millimeters.Example 4: Invalid Choice
Input:
- Choice:
4
- Meters:
100
Output:
1234567Unit Conversion: Length (Meters to other units)1. Convert to Kilometers2. Convert to Centimeters3. Convert to MillimetersEnter your choice (1-3): 4Enter the length in meters: 100Invalid choice!
- Choice:
- The program calculates the result based on the conversion and prints it to the console.