Write a program like that of the previous exercise that converts from meters and centimeters into feet
and inches. Use functions for the subtasks.
PROGRAM CODE:
//Header section
#include <iostream>
using namespace std;
// Function to convert meters and centimeters to feet
double convertToFeet(int meters, int centimeters)
{
double totalInches = (meters * 100 + centimeters) / 2.54; // Convert to total inches
double feet = totalInches / 12; // Convert to feet
return feet;
}
// Function to convert meters and centimeters to inches
double convertToInches(int meters, int centimeters) {
double totalInches = (meters * 100 + centimeters) / 2.54; // Convert to total inches
return totalInches;
}
// Function to convert feet to inches
double convertFeetToInches(double feet) {
double inches = feet * 12;
return inches;
}
// Function to display the conversion result
void displayResult(int meters, int centimeters, double feet, double inches) {
cout << "The length " << meters << " meters and " << centimeters << " centimeters is equivalent to:\n";
cout << "Feet: " << feet << " ft\n";
cout << "Inches: " << inches << " in\n";
}
int main() {
int meters, centimeters;
// Get input from the user
cout << "Enter the length in meters: ";
cin >> meters;
cout << "Enter the length in centimeters: ";
cin >> centimeters;
// Convert meters and centimeters to feet and inches
double feet = convertToFeet(meters, centimeters);
double inches = convertToInches(meters, centimeters);
// Display the result
displayResult(meters, centimeters, feet, inches);
return 0;
}
OUTPUT OF THE PROGRAM CODE:
Enter the length in meters: 5
Enter the length in centimeters: 500
The length 5 meters and 500 centimeters is equivalent to:
Feet: 32.8084 ft
Inches: 393.701 in