Chapter three programming problems of c++ prime plus

Keywords: Programming

The third chapter

1 foot inch, conversion, conversion factor in const

#include <iostream>
using namespace std;

int main()
{
    const int inch_per_foot = 12;
    int height = 0;
    cout << "Enter your height in feet:___" << "\b\b\b";
    cin >> height;
    cout << "Your height is:" << height << "Foot" << endl;
    cout << "Total" << height/inch_per_foot << "Foot" <<
         height%inch_per_foot << "inch" << endl; 

    return 0;
}

2 input height in a few feet and inches, input the same weight in pounds, and output BMI

#include <iostream>
using namespace std;

int main()
{
    const double inch_per_foot = 12;
    const double metre_per_inch = 0.0254;
    const double pound_per_kilogram = 2.2;
    double height_foot{0},height_inch{0},weight_pound{0};
    double height_m{0},weight_kg{0};

    cout << "Enter feet and inches for height:"<< endl;
    cin >> height_foot >> height_inch;
    height_m = (height_foot * 12 + height_inch) * metre_per_inch;
    cout << "Enter pounds of weight:" << endl;
    cin >> weight_pound;
    weight_kg = weight_pound / pound_per_kilogram;
    double BMI = (weight_kg) / (height_m * height_m);
    cout << "Your height is:" << height_m << "rice" << endl
         << "Your weight is:" << weight_kg << "Kilogram" << endl
         << "Your BMI Yes," << BMI << endl;
    return 0;
}

3 enter a latitude in degrees, minutes and seconds and display it in degrees

#include <iostream>
using namespace std;

int main()
{
    cout << "Entet a latitude in degrees, minutes, and seconds: " << endl;

    cout << "First, enter the degrees: " << endl;
    int degrees = 0;
    cin >> degrees;

    cout << "Next, enter the minutes of arc: " << endl;
    int minutes = 0;
    cin >> minutes;

    cout << "Finally, enter the seconds of arc: " << endl;
    int seconds = 0;
    cin >> seconds;
    
    double result = 0;
    result = seconds / 3600.0 + minutes / 60.0 + degrees;
    
    cout << degrees << " degrees, " 
         << minutes << " minutes, "
         << seconds << " seconds = "
         <<  result << " degrees."
         << endl;
    
    return 0;
}

4 enter the number of seconds as an integer (stored in long or long long) and display them as days, hours, minutes, and seconds

#include <iostream>
using namespace std;

int main()
{
    const int hours_per_day = 24;
    const int minutes_per_hours = 60;
    const int seconds_per_minutes = 60;
    cout << "Enter the number of seconds: ";
    long long seconds = 0;
    cin >> seconds;

    int minutes,hours,days;
    minutes = seconds / seconds_per_minutes;
    seconds = seconds % seconds_per_minutes;

    hours = minutes / minutes_per_hours;
    minutes = minutes % minutes_per_hours;

    days = hours / hours_per_day;
    hours = hours % hours_per_day;

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

    return 0;
}

5 input global population and population of a country, output percentage

#include <iostream>
using namespace std;

int main()
{
    cout << "Enter the world's population: ";
    long long int world_population = 0;
    cin >> world_population;

    cout << "Enter the population of the US: ";
    long long int nation_population = 0;
    cin >> nation_population;

    double percent = 0;
    percent = 100 * (double(nation_population) / double(world_population));
    cout << "The population of the US is " 
         << percent << '%'
         << " of the world population." << endl;
    return 0;
}

6 enter mileage (miles) and gas usage (gallons) and indicate the vehicle's fuel consumption (miles per gallon or liters per 100km)

#include <iostream>
using namespace std;

int main()
{
    int type = 0;
    cout << "miles of km?\nmiles : 0 and km : 1" << endl;
    cin >> type;
    double males = 0;
    double galens = 0;
    double km = 0;
    double litre = 0;
    switch (type)
    {
        case 0:
            cout << "Enter miles and gallons,Space partition: \n";
            cin >> males >> galens;
            cout << "Your car can run on a gallon of gas " 
                 << males / galens << " Mile.\n";
            break;
        case 1:
            cout << "Input km and l,Space partition: \n";
            cin >> km >> litre;
            cout << "Your car's fuel consumption is:  " 
                 << litre / (km / 100)  << " rise"
                 << endl;
            break;
        default:
            break;
    }
    return 0;
}

7 input fuel consumption (liter) in 100km and convert to miles per gallon

#include <iostream>
using namespace std;

int main()
{
    const double mile_per_km = 0.6214;
    const double liter_per_galen = 3.875;
    cout << "Input style selection:\n European style press 0,Press 1 for American style: ";
    int type = 0;
    cin >> type;
    double km{0},liter{0},mile{0},galen{0};
    switch (type)
    {   
        case 0:
            cout << "Input fuel consumption(rise)Mileage(kilometre): ";
            cin >> liter >> km;
            cout << "Fuel consumption per 100 km: " << liter/(km/100) << endl;
            galen = liter/liter_per_galen;
            mile = km*mile_per_km;
            cout << "Equivalent to " 
                 << mile/galen  << "Miles per gallon"
                 << endl;
            break;
        case 1:
            cout << "Enter mileage(Mile)Fuel consumption(gallon): ";
            cin >> mile >> galen;
            cout << "Gallons per gallon " << mile/galen 
                 << " Mile" << endl;
            liter = galen*liter_per_galen;
            km = mile/mile_per_km;
            cout << "Equivalent to fuel consumption per 100 km " << 100*liter/km << "rise" << endl;
            break;
        default:
            break;
    }
    return 0;
}

Posted by gmbot on Sat, 16 Nov 2019 09:48:20 -0800