C++ Programming Exercises: Unit Conversions and Calculations

1. Height Conversion from Inches to Feet and Inches

Write a program that prompts the user to input their height in inches, then converts it to feet and inches. Use an underscore character to indicate the input position and a const symbolic constant for the conversion factor.

#include <iostream>
using namespace std;

int main() {
    int totalInches, remainingInches, feet;
    const int INCHES_PER_FOOT = 12;

    cout << "Enter your height in inches: ____\b\b\b\b ";
    cin >> totalInches;

    feet = totalInches / INCHES_PER_FOOT;
    remainingInches = totalInches % INCHES_PER_FOOT;
    cout << "Your height is " << feet << " feet, " << remainingInches << " inches" << endl;

    return 0;
}

2. BMI Calculation from Height and Weight

Create a program that takes height in feet and inches and weight in pounds, then calculates and displays the Body Mass Index (BMI). Use symbolic constants for conversion factors.

#include <iostream>
using namespace std;

int main() {
    float heightFeet, heightInches, weightPounds;
    float heightMeters, weightKilograms, bmi;
    const double INCHES_PER_FOOT = 12.0;
    const double METERS_PER_INCH = 0.0254;
    const double POUNDS_PER_KILOGRAM = 2.2;

    cout << "Enter your height in feet and inches: __ __\b\b\b\b\b";
    cin >> heightFeet >> heightInches;
    cout << "Enter your weight in pounds: ___\b\b\b";
    cin >> weightPounds;

    weightKilograms = weightPounds / POUNDS_PER_KILOGRAM;
    heightMeters = (heightFeet * INCHES_PER_FOOT + heightInches) * METERS_PER_INCH;
    bmi = weightKilograms / (heightMeters * heightMeters);

    cout << "Weight in kilograms: " << weightKilograms << endl;
    cout << "Height in meters: " << heightMeters << endl;
    cout << "BMI: " << bmi << endl;

    return 0;
}

3. Latitude Conversion from Degrees, Minutes, Seconds to Decimal Degrees

Develop a program that accepts latitude in degrees, minutes, and seconds, then converts it to decimal degrees. Use symbolic constants and separate variables for each input.

#include <iostream>
using namespace std;

int main() {
    int degrees, minutes, seconds;
    double decimalDegrees;
    const double MINUTES_PER_DEGREE = 60.0;
    const double SECONDS_PER_MINUTE = 60.0;

    cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
    cout << "First, enter the degrees: ";
    cin >> degrees;
    cout << "Second, enter the minutes of arc: ";
    cin >> minutes;
    cout << "Finally, enter the seconds of arc: ";
    cin >> seconds;

    decimalDegrees = degrees + (minutes + seconds / SECONDS_PER_MINUTE) / MINUTES_PER_DEGREE;
    cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << decimalDegrees << " degrees" << endl;

    return 0;
}

4. Time Conversion from Seconds to Days, Hours, Minutes, and Seconds

Write a program that inputs a duration in seconds (using a long long variable) and outputs it in days, hours, minutes, and seconds. Use symbolic constants for conversion factors.

#include <iostream>
using namespace std;

int main() {
    long long totalSeconds;
    int days, hours, minutes, seconds;
    const int HOURS_PER_DAY = 24;
    const int MINUTES_PER_HOUR = 60;
    const int SECONDS_PER_MINUTE = 60;

    cout << "Enter the number of seconds: ";
    cin >> totalSeconds;

    seconds = totalSeconds % SECONDS_PER_MINUTE;
    minutes = (totalSeconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
    hours = ((totalSeconds / SECONDS_PER_MINUTE) / MINUTES_PER_HOUR) % HOURS_PER_DAY;
    days = ((totalSeconds / SECONDS_PER_MINUTE) / MINUTES_PER_HOUR) / HOURS_PER_DAY;

    cout << totalSeconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;

    return 0;
}

5. Population Percentage Calculation

Create a program that inputs the global population and the population of a specific country (e.g., US or China) using long long variables, then calculates and displays the percentage of the global population represented by that country.

#include <iostream>
using namespace std;

int main() {
    long long worldPopulation, countryPopulation;

    cout << "Enter the world's population: ";
    cin >> worldPopulation;
    cout << "Enter the population of the country: ";
    cin >> countryPopulation;

    double percentage = 100.0 * static_cast<double>(countryPopulation) / static_cast<double>(worldPopulation);
    cout << "The population of the country is " << percentage << "% of the world population." << endl;

    return 0;
}

6. Fuel Efficiency Calculator in Imperial and Metric Units

Write a program that calculates fuel efficiency. It first accepts distence in miles and fuel in gallons to compute miles per gallon. Optionally, it can accept distance in kilometers and fuel in liters to compute liters per 100 kilometers.

#include <iostream>
using namespace std;

int main() {
    double distanceMiles, fuelGallons;

    cout << "Enter distance traveled (miles): ";
    cin >> distanceMiles;
    cout << "Enter fuel used (gallons): ";
    cin >> fuelGallons;

    double mpg = distanceMiles / fuelGallons;
    cout << "Fuel efficiency: " << mpg << " miles per gallon" << endl;

    char choice;
    cout << "Convert to metric units? (y/n): ";
    cin >> choice;

    if (choice == 'y' || choice == 'Y') {
        double distanceKm, fuelLiters;
        cout << "Enter distance traveled (kilometers): ";
        cin >> distanceKm;
        cout << "Enter fuel used (liters): ";
        cin >> fuelLiters;

        double lPer100Km = (fuelLiters / distanceKm) * 100.0;
        cout << "Fuel consumption: " << lPer100Km << " liters per 100 kilometers" << endl;
    }

    return 0;
}

7. Conversion Between European and US Fuel Consumption Standards

Develop a program that converts fuel consumption from European style (liters per 100 kilometers) to US style (miles per gallon). Use constants for conversion factors.

#include <iostream>
using namespace std;

int main() {
    double litersPer100Km;
    const double MILES_PER_100_KM = 62.14;
    const double LITERS_PER_GALLON = 3.875;

    cout << "Enter fuel consumption (liters per 100 kilometers): ";
    cin >> litersPer100Km;

    double mpg = MILES_PER_100_KM / (litersPer100Km / LITERS_PER_GALLON);
    cout << "Equivalent US fuel efficiency: " << mpg << " miles per gallon" << endl;

    return 0;
}

Tags: C++ programming unit conversion BMI calculation fuel efficiency

Posted on Tue, 09 Jun 2026 17:21:06 +0000 by contex