Answer:
The c++ program is given below.
#include <iostream>
using namespace std;
int main() {
double w1, p1, w2, p2, cost1, cost2;
cout << "Enter the weight of first sugar packet" << endl;
cin >> w1;
cout << "Enter the price of first sugar packet" << endl;
cin >> p1;
cout << "Enter the weight of second sugar packet" << endl;
cin >> w2;
cout << "Enter the price of second sugar packet" << endl;
cin >> p2;
cost1 = p1/w1;
cost2 = p2/w2;
if(cost1 < cost2)
cout << "Sugar in first package has better price." << endl;
else if(cost1 > cost2)
cout << "Sugar in second package has better price." << endl;
else if(cost1 == cost2)
cout << "Sugar in both packages has same price." << endl;
return 0;
}
OUTPUT
Enter the weight of first sugar packet
2
Enter the price of first sugar packet
45
Enter the weight of second sugar packet
3
Enter the price of second sugar packet
65
Sugar in second package has better price.
Step-by-step explanation:
This program compares the unit cost of two sugar packages.
This program is designed to take user input but the user input is not validated since this is not mentioned in the question.
1. The double variables are declared for weights and prices for sugar in two packages.
2. Also, double variables are declared to hold the calculated values of unit cost per price for both the packages.
double w1, p1, w2, p2, cost1, cost2;
3. The program takes user input for weight and price for two sugar packages.
4. The unit cost for both sugar packages is calculated.
cost1 = p1/w1;
cost2 = p2/w2;
5. The unit cost for both packages are compared and the message is displayed on the output accordingly.
6. If the unit cost for both packages are the same, the message is displayed on the output accordingly.
7. The program ends with a return statement.