Here are collected the task to the article Data types, variables and constants. Try to solve problems on their own and only need to look at the decision. So you will learn much faster programming fundamentals.
1. Declare variables with which you can calculate the total amount of the purchase of several products. For example a chocolate bar, coffee and packets of milk.
View code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intchocolate=2;// хранит количество упаковок
intmilk=3;
intcoffee=1;
floatpriceOfChocolate=11.04;// хранит цены за одну упаковку
2. Declare three variables of type int, and the first to assign a numeric value, the second variable is the first variable increased by 3, and the third variable is equal to the sum of the first two.
View code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
usingnamespacestd;
intmain()
{
intfirst=4;
intsecond=first+3;
intthird=first+second;
cout<<"first = "<<first<<endl;
cout<<"second = "<<second<<endl;
cout<<"third = "<<third<<endl;
return0;
}
3. Declare variables, to count the total number of objects for the table. for example cups, the same number of saucers and spoons.
View code
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intcups=6;// количество чашек
intspoons=cups;// количество ложек равно кол-тву чашек
intsaucers=cups;// блюдца
// или можно так записать
// int cups, spoons, saucers;
// cups = spoons = saucers = 6;
intamount=cups+spoons+saucers;// общее количество
cout<<"Всего "<<amount<<" предметов"<<endl;
return0;
}
4.6
160
130 thoughts on “Tasks: Data types, variables and constants”
#include using namespace std;
int main() { int milk = 11, coffee = 29, chocolate = 33;
/* Declare variables with which you can calculate the total amount of purchases of several products. For example a chocolate bar, coffee and milk cartons */
// Declare three variables of type int and assign // first numeric value, second variable // equal to the first variable increased by 3, // and the third variable is equal to the sum of the first two.
#include using namespace std;
int main() { setlocale(LC_ALL, “rus”); int a, b, c; a = 5; cout << "Первая переменная int: " << a << endl; b = a * 3; cout << "Вторая переменная int: " << b <<endl; c=a+b; cout << "Третья переменная int: " << c;
// Declare variables, to count the total // table setting items. for example cups, // the same number of saucers and spoons.
#include using namespace std;
int main() { setlocale(LC_ALL, “rus”); int a, b, c, d; //a – cup, b – saucer, c – spoons, d – general. a = 4; b = a; c = b; d=a+b+c; cout << "Количесттво чашек: " << a << endl; cout << "Количество блюдец: " << b << endl; cout << "Количество ложек: " << c << endl; cout << "Общее количество предметов " << d << " for table setting!"; return 0; }
I did a little of my own, so that the number of products is not limited (nearly , How long will the int variable type be enough?). #include using namespace std;
int main() { cout <> productQuantity; int x=1; // Product number for While loop + to output to the console double z = 0.0; //Variable for initial price while (x != (productQuantity+1)) { double y; //Product Variable
cout << "Please enter the price of the " << x <> Y; z = z + Y; ++x;
int main() { setlocale(LC_ALL, “Rus”); int milk; cout<<"Сколько вам нужно молока?"<>milk; int chokolate; cout<<"Сколько вам нужно Шоколада?"<>chokolate; int coffee; cout<<"Сколько вам нужно кофе?"<>coffee;
#include
using namespace std;
int main()
{
int milk = 11, coffee = 29, chocolate = 33;
int sum = milk + coffee + chocolate;
cout << "milk = " << milk << "$" << endl;
cout << "coffee = " << coffee << "$" << endl;
cout << "chocolate = " << chocolate << "$" << endl;
cout << "Total = "<< sum << "$" << endl;
return 0;
}
Wrong
#include
using namespace std;
int main() {
setlocale(LC_ALL, “rus”);
int a;
int b;
int c;
int tile;
int cofe;
int generation;
cout << "Введите цену (chocolate braid)" <> tile;
cout << "Введите число" <> a;
cout << "Введите цену (Coffee)" <> cofe;
cout << "Введите число" <> b;
cout << "Введите цену (Milk)" <> moloko;
cout << "Введите число" <> c;
int sum = 0;
sum = a * tile + b * cofe + moloko * c;
cout << "Общая цена ";
cout << sum << endl;
}
#include
#include
#include
using namespace std;
int main()
{
//Russian
setlocale(0, “”);
//variables
float chocolate, milk, coffee;
float chocolatecost, milkcost, coffeecost;
float chocolatesum, milksum, coffeesum;
float sum;
//number of packages
cout << "Введите количество пачек шоколада: " <> chocolate;
cout << "Введите количество пачек молока: " <> milk;
cout << "Введите количество пачек кофе: " <> coffee;
//price
cout << "Введите стоимость пачки шоколада: " <> chocolatecost;
cout << "Введите стоимость пачки молока: " <> milkcost;
cout << "Введите стоимость пачки кофе: " <> coffeecost;
//calculations
chocolatesum = chocolatecost * chocolate;
milksum = milkcost * milk;
coffeesum = coffeecost * coffee;
sum = milksum + coffeesum + chocolatesum;
cout << "Общая цена шоколада: " << chocolatesum << endl;
cout << "Общая цена молока: " << milksum << endl;
cout << "Общая цена кофе: " << coffeesum << endl;
cout << "Общая сумма: " << sum << endl;
}
/* Declare variables with which you can
calculate the total amount of purchases of several products.
For example a chocolate bar, coffee and milk cartons */
#include
using namespace std;
int main() {
setlocale(LC_ALL, “rus”);
int product_chocolateBar = 30;
int product_coffee = 100;
int product_milkPackages = 60;
int purchase_amount;
cout << "Сумма покупки плитки шоколада: " << product_chocolateBar << " rub." << endl;
cout << "Сумма покупки баночки кофе: " << product_coffee << " rub." << endl;
cout << "Сумма покупки пакеты молока: " << product_milkPackages << " rub." << endl;
purchase_amount = product_chocolateBar + product_coffee+product_milkPackages;
cout << endl;
cout << "Сумма всех покупок: " << purchase_amount << " rub." << endl;
cout << endl;
return 0;
}
// Declare three variables of type int and assign
// first numeric value, second variable
// equal to the first variable increased by 3,
// and the third variable is equal to the sum of the first two.
#include
using namespace std;
int main() {
setlocale(LC_ALL, “rus”);
int a, b, c;
a = 5;
cout << "Первая переменная int: " << a << endl;
b = a * 3;
cout << "Вторая переменная int: " << b <<endl;
c=a+b;
cout << "Третья переменная int: " << c;
return 0;
}
// Declare variables, to count the total
// table setting items. for example cups,
// the same number of saucers and spoons.
#include
using namespace std;
int main() {
setlocale(LC_ALL, “rus”);
int a, b, c, d;
//a – cup, b – saucer, c – spoons, d – general.
a = 4; b = a; c = b; d=a+b+c;
cout << "Количесттво чашек: " << a << endl;
cout << "Количество блюдец: " << b << endl;
cout << "Количество ложек: " << c << endl;
cout << "Общее количество предметов " << d << " for table setting!";
return 0;
}
I did a little of my own, so that the number of products is not limited (nearly , How long will the int variable type be enough?).
#include
using namespace std;
int main()
{
cout <> productQuantity;
int x=1; // Product number for While loop + to output to the console
double z = 0.0; //Variable for initial price
while (x != (productQuantity+1))
{
double y; //Product Variable
cout << "Please enter the price of the " << x <> Y;
z = z + Y;
++x;
}
cout << "Total amount: = " << from;
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Rus”);
int milk;
cout<<"Сколько вам нужно молока?"<>milk;
int chokolate;
cout<<"Сколько вам нужно Шоколада?"<>chokolate;
int coffee;
cout<<"Сколько вам нужно кофе?"<>coffee;
double MilkPrice = 55.99;
double ChocolatePrice = 49.99;
double CoffeePrice = 22.50;
double sum = (milk*MilkPrice + chokolate*ChocolatePrice + coffee*CoffeePrice);
cout<<"С вас "<<"₽"<<sum<<" ,Thank you for your purchase!!!"<<endl;
}