Algorithms and Programming Lab Lesson 3
QUESTION 1
A metric ton id 35273,92 ounces.Write a program that will read the weight of package of breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield
1 metric yon of cereal.Your program should allow the user to repeat this calculation as often as the user wishes.
ANSWER
#include <iostream>
using namespace std;
int main()
{
const double conversionFactor = 35273.92;
double weightInOunces = 0.0;
double weightInMetricTons = 0.0;
double weightInUpToNow = 0.0;
int counter = 0;
char userInput;
do{
cout <<"Please enter the weight of o box of cereal in ounces: ";
cin >> weightInOunces;
weightInMetricTons = weightInOunces / conversionFactor;
while( weightInUpToNow <1 )
{
weightInUpToNow += weightInMetricTons;
counter = counter+ 1;
}
cout << "The number of boxes you need is ";
cout << counter;
cout << "to have one metric ton of cereal." << endl;
counter = 0;
weightInUpToNow = 0.0;
cout << "Do you want to start again(y/n)?";
cin >> userInput;
}
while (userInput == 'y' || userInput == 'Y');
return 0;
}
Comments
Post a Comment