C++ Program: Write a program that will read in a weight in kilograms and outputs
ID: 3810166 • Letter: C
Question
C++ Program: Write a program that will read in a weight in kilograms and outputs the equivalent weight in pounds and ounces. Express the pounds as an integer and ounces to the nearest 1/100 of an ounce. You have to write and use a function that uses the following function prototype to perform the calculations:
- void KgtoLbOz(double kg, int& lbs, double& oz);
The funtion should not input any data or print any results. It should only do the calculation. Include a loop that lets the user repeat the computation for any new input values until the user decides they want to end the program.
NOTE: There are 2.2046 pounds in a kilogram and 16 ounces in a pound.
Explanation / Answer
#include <iostream>
using namespace std;
void KgtoLbOz(double kg, int& lbs, double& oz) {
lbs = kg * 2.2046;
oz = lbs * 16 ;
}
int main()
{
double kg, oz;
int lbs;
char ch='y';
while(ch =='y' || ch =='Y'){
cout << "Enter the weight of the object in kilogram: ";
cin >> kg;
KgtoLbOz(kg, lbs, oz);
cout<<"The weight of the object is "<<lbs<<" pounds. "<<oz<<" ounces"<<endl;
cout<<"Would you like to run the program again (Y or N): ";
cin >> ch;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the weight of the object in kilogram: 16.5315
The weight of the object is 36 pounds. 576 ounces
Would you like to run the program again (Y or N): y
Enter the weight of the object in kilogram: 0.876
The weight of the object is 1 pounds. 16 ounces
Would you like to run the program again (Y or N): n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.