The Site is under maintenance!..Test link

Assignment1_3

Variable, Operator, and Expression [Set – 3]

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:
  1. Get the length in feet and inches.
  2. Convert the length into total inches.
  3. Convert total inches into centimeters.
  4. 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;
}