8. Program that inputs a value and type of conversion. The program should then output the value after conversion.

Chapter 5: 

Conditional Structures

Programming Exercise

Problem # 8:

Write a program that inputs a value and type of conversion. The program should then output the value after conversion. The program should include the following conversions:
1 inch = 2.54 centimeters
1 gallon = 3.785 liters
1 mile = 1.609 kilometers
1 pound = 0.4536 kilograms
Make sure that program accepts only valid choices for type of conversion to perform.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    char conv;
    float res, in;
    cout<<"Choose the type of conversion"<<endl;
    cout<<"'A' to convert inch into centimenters \n'B' to convert gallon into liters \n'C' to convert mile into kilometers \n'D' to convert pound into kilograms";
    cin>>conv;
  
    if (conv=='A'||conv=='a')
    {
        cout<<"You choose to convert inch into centimeters"<<endl;
        cout<<"Enter the lengthe in inches: ";
        cin>>in;
        res= 2.54 * in;
        cout<<in<<" inches = "<<res<<" cm"<<endl;
    }
    else if (conv=='B' || conv=='b')
    {
        cout<<"You choose to convert gallons into liters"<<endl;
        cout<<"Enter the value in gallons: ";
        cin>>in;
        res= 3.785 * in;
        cout<<in<<" gallons = "<<res<<" l"<<endl;
    }
    else if (conv=='C' ||conv=='c')
    {
        cout<<"You choose to convert miles into kilometers"<<endl;
        cout<<"Enter lenght in miles: ";
        cin>>in;
        res= 1.609 * in;
        cout<<in<<" miles = "<<res<<" km"<<endl;
    }
    else if (conv=='D' ||conv=='d')
    {
        cout<<"You choose to convert pounds into kilograms"<<endl;
        cout<<"Enter the weight in pounds: ";
        cin>>in;
        res= 0.4536 * in;
        cout<<in<<" pounds = "<<res<<" kg"<<endl;
    }
    else
        cout<<"invalid input..";
    return     0;
}

OR

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int c;
    float res, n;
    cout<<"Enter the type of conversions:\n1. To convert into centimeters.\n2. To convert into liters.\n3. To convert into kilometers.\n4. To convert into kilograms.";
    cin>>c;
    switch(c)
    {
        case 1:
            cout<<"\nEnter value in inches:";
            cin>>n;
            res=2.54*n;
            break;
        case 2:
            cout<<"\nEnter value in gallon:";
            cin>>n;
            res=3.785*n;
            break;
        case 3:
            cout<<"\nEnter the value in mile:";
            cin>>n;
            res=1.609*n;
            break;
        case 4:
            cout<<"Enter the value in pound:";
            cin>>n;
            res=0.4536*n;
            break;
        default:
            cout<<"Invalid input.";
    }
    cout<<"\nThe value after conversion is:"<<res;
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
7. Program inputs code for movie type and display its category.

Comments