Variable, Operator, and Expression [Set – 3]
Problem Statement:
Write a program that takes length as input in feet and inches. The program should then convert the lengths in centimeters and display it on screen. Assume that the given lengths in feet and inches are integers.
Algorithm:
- Get the length in feet and inches.
- Convert the length into total inches.
- Convert total inches into centimeters.
- Output the length in centimeters.
Answer:
#include <iostream>
using namespace std;
// Named constants
const double INCH_TO_CM = 2.54;
const int FEET_TO_INCHES = 12;
int main() {
int feet, inches, totalInches;
double centimeters;
// Input lengths in feet and inches
cout << "Enter length in feet: ";
cin >> feet;
cout << "Enter length in inches: ";
cin >> inches;
// Convert to total inches
totalInches = feet * FEET_TO_INCHES + inches;
// Convert to centimeters
centimeters = totalInches * INCH_TO_CM;
// Output the length in centimeters
cout << "Length in centimeters: " << centimeters << " cm" << endl;
return 0;
}