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.7
155
126 thoughts on “Tasks: Data types, variables and constants”
#include using namespace std; int main() { int chocolate, coffe; int milk, summ;
cout << "Вы вводите первое чилсо, а второе число будет увеличино на 3, затем третие число будет ровняться сумме двух чисел.\n";
cout <> a;
b = a * 3; c = a + b;
cout << "Первое число равняется: " << a << "\n"; cout << "Второе число равняется: " << b << "\n"; cout << "Сумма двух чисел равняется: " << c << "\n"; }
Answer to 2 the task: #include using namespace std; int main () { int a = 11; int b = a + 3; int c = a + b; cout << "Result A: " << a << endl; cout << "Result B: " << b << endl; cout << "Result C: " << c << endl; return 0; }
#include
using namespace std;
int main()
{
int chocolate, coffe;
int milk, summ;
chocolate = 50;
coffee = 100;
milk = 65;
cout << "Шоколад " << chocolate << " руб." <<'\n';
cout << "Кофе " << coffe << " руб." <<'\n';
cout << "Молоко " << milk << " руб." <<'\n';
summ = chocolate + coffe + milk;
cout << "Сумма всех товаров: " << summ << " руб." <<'\n';
return 0;
}
215
#include
#include
using namespace std;
int main() {
setlocale(0, “rus”);
int chocolate, milk, coffee;
int summ;
chocolate = 75;
milk = 25;
kofe = 50;
summ = milk + coffee + chocolate;
cout << "шоколад – 75 р\nмолоко – 25 р\nкофе – 50 _" << endl;
cout << "общая сумма = "<<summ<<" р";
return 0;
}
vector products;
vector price;
float a;
int count;
int coutn_product;
cout <> count;
products.resize(count);
price.resize(count);
for (int i = 0; i < products.size(); i )
{
cout << "Количество товара #" << i <> coutn_product;
products[i] = coutn_product;
}
for (int i = 0; i < price.size(); i )
{
cout << "Цена за 1 шт. товара #" << i <> a;
price[i] = a;
}
for (int i = 0; i < count; i )
{
summ += products[i] * price[i];
}
cout << summ;
Что за сложная конструкция для легкой задачи?
setlocale(LC_ALL, “rus”);
int chocolate, coffee;
int milk, summ;
chocolate = 50; //цена
coffee = 100; //цена
milk = 75; //цена
int ammount_chocolate, ammount_coffee;
int ammount_milk;
ammount_chocolate = 3;//количество
ammount_coffee = 3;//количество
ammount_milk = 3;//количество
cout << "Шоколад" << chocolate << "руб " << ammount_chocolate << "x" << endl;
cout << "Кофе" << coffee << "руб " << ammount_coffee << "x" << endl;
cout << "Молоко" << milk << "руб " << ammount_milk << "x" << endl;
summ = (chocolate * ammount_chocolate) + (milk * ammount_milk) + (coffee * ammount_coffee);
cout << summ;
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Russian”);
int choco, cofe, milk, sum;
cout <> choco;
cout <> cofe;
cout <> milk;
summa = (choco * 56) + (cofe * 50) + (milk * 182);
cout << "\nВыши покупки обошлись вам в: " << sum << " руб.";
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Russian”);
int choco, cofe, milk, sum;
cout <> choco;
cout <> cofe;
cout <> milk;
summa = (choco * 56) + (cofe * 50) + (milk * 182);
cout << "\nВыши покупки обошлись вам в: " << sum << " руб.";
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Russian”);
int a, b, c;
cout << "Вы вводите первое чилсо, а второе число будет увеличино на 3, затем третие число будет ровняться сумме двух чисел.\n";
cout <> a;
b = a * 3;
c = a + b;
cout << "Первое число равняется: " << a << "\n";
cout << "Второе число равняется: " << b << "\n";
cout << "Сумма двух чисел равняется: " << c << "\n";
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Russian”);
int cups, saucers, spoons, all;
cout <> cups;
cout <> saucers;
cout <> spoons;
all = cups + saucers + spoons;
cout << "\nУ вас общего количества предметов для сервировки стола ровняется: " << all;
}
Answer to 1 the task:
#include
#include
using namespace std;
int main () {
setlocale(LC_ALL, “Russian”);
int choco, coffe, milk, result;
cin >> choco >> coffe >> milk;
result = (choco + coffe + milk);
cout << "Общая сумма всех продуктов: " << result << endl;
return 0;
}
Answer to 2 the task:
#include
using namespace std;
int main () {
int a = 11;
int b = a + 3;
int c = a + b;
cout << "Result A: " << a << endl;
cout << "Result B: " << b << endl;
cout << "Result C: " << c << endl;
return 0;
}
А почему int b=a+3, а не int b=a*3?