106k views
4 votes
Write a program that will read in aweight in kilograms and grams and will output the equivalent weightin pounds and ounces. Use at least three functions: one for input,one or more for calculating, and one for output. Include a loopthat lets the user repeat this computation for new input valuesuntil the user says he or she wants to end the program. There are2.2046 pounds in a kilogram, 1,000 grams in a kilogram, and 16ounces in a pound.

User Aleadam
by
5.6k points

1 Answer

3 votes

C++ program for converting kilograms and grams to pounds and ounces

#include <iostream>

using namespace std;

void computation(double kilog, double gram, double& pon, double& oun) /*defining function for computing*/

{

pon = kilog*2.2046;/*Formula for converting*/

oun= gram*0.03527;

}

void output(double pon,double oun)/*For printing output*/

{

cout << "Pounds are: " << pon << endl;

cout << "Ounce are: " << oun << endl;

}

void input(double &kilog,double&gram)/*for taking output*/

{

cout << "Enter kilograms to convert: ";

cin >> kilog;

cout << "Enter grams to convert: ";

cin >> gram;

}

int main() //driver function

{

double kilog = 0.0,gram = 0.0,oun=0.0,pon=0.0;//Initializing

int choice = 0;

while (1) {

cout << "Enter- 1 for converting kilogram and grams to pounds and ounces" << endl;

cout << "Enter-2 for exiting" << endl;

cout << "Please Enter choice: ";/*Asking choice from user*/

cin >> choice;

if (choice == 1) {

input(kilog,gram);/*Calling function for taking input*/

computation(kilog, gram, pon, oun); /*calling function for computing*/

output(pon,oun);/*calling function for output*/

}

else {

return 0;

}

}

}

Output

Enter- 1 for converting kilogram and grams to pounds and ounces

Enter-2 for exiting

Please Enter choice:1

Enter kilograms to convert:20

Enter grams to convert:10

Pounds are: 44.092

Ounce are: 0.3527

Enter- 1 for converting kilogram and grams to pounds and ounces

Enter-2 for exiting

Please Enter choice: 2

User Sasindu Lakshitha
by
5.0k points