Допоможіть будьласочка, дуже потрібно..
Ввести з клавіатури значення ( ціну за кг цукерок ). Вивести ціну за 1.2, 1.4, 1.6 ... 2 кг цукерок. Записати рішення в трьох видах циклу ( for, while, do while )
Ответы
Ответ:
(Язык С++)
(for)
#include <iostream>
using namespace std;
int main() {
double price;
cout << "Enter the price per kg of candy: ";
cin >> price;
for (double weight = 1.2; weight <= 2; weight += 0.2) {
double cost = weight * price;
cout << "The price for " << weight << " kg of candy is $" << cost << endl;
}
return 0;
}
(while)
#include <iostream>
using namespace std;
int main() {
double price;
cout << "Enter the price per kg of candy: ";
cin >> price;
double weight = 1.2;
while (weight <= 2) {
double cost = weight * price;
cout << "The price for " << weight << " kg of candy is $" << cost << endl;
weight += 0.2;
}
return 0;
}
(do while)
#include <iostream>
using namespace std;
int main() {
double price;
cout << "Enter the price per kg of candy: ";
cin >> price;
double weight = 1.2;
do {
double cost = weight * price;
cout << "The price for " << weight << " kg of candy is $" << cost << endl;
weight += 0.2;
} while (weight <= 2);
return 0;
}