Write a program that implements unit conversion for the following pairs: (1) lbs -> kg, (2) miles -> and (3) Fahrenheit -> Celsius. Each conversion should be implemented using a function with has no return value. The main program will ask the user for a number to indicate which conversion they want to perform. Within the body of each function the user will then be prompted for the value to be converted. The results of the conversion should be displayed within the function
// A program for unit conversion
#include<iostream>
using namespace std;
function1() // Function for converting from lbs to Kg
{
float a,b;
cout<<"Enter a unit in lbs \n ";
cin>>a;
b=a*0.453592374495299;
cout<<"The result is "<<b;
}
function2() // Function for converting from Miles to Kms
{
float a,b;
cout<<"Enter a unit in miles\n ";
cin>>a;
b=a*1.609;
cout<<"The result is "<<b;
}
function3() //Function for converting from fahrenheit to degree celcius
{
float a,b;
cout<<"Enter a unit in fahrenheit\n ";
cin>>a;
b=(a-32.0) * 5.0 / 9.0;
cout<<"The result is "<<b;
}
int main()
{
int option;
cout<<"1. lbs to Kg conversion \n2. Miles to Kms\n3. Fahrenheit to degree celcius";
cout<<"\nChoose an operation\n";
cin>>option;
switch(option)
{
case 1:
function1();
break;
case 2:
function2();
break;
case 3:
function3();
break;
default:
cout<<"Invalid choice";
}
return 0;
}
Comments
Leave a comment