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() { setlocale(LC_ALL, “rus”); cout << "Пишем нашу первую программу" << endl; // creating change int March = 11; int cofe = mars + 3; int milk = mars + cofe; //price of Mars and coffee int summa = 0; cout << "обшая сумма затрат-" << sum << endl; // data display cout << " the value of chocolates" << mars << endl; cout << "стоймость кофе" << cofe << endl; cout << "стоймость балона молока" << milk << endl; cout << endl; // go one more line // calculation of the total cost of the rogue sum = march + cofe + milk; // display result on screen cout << " total spent" << sum << "рублей" << endl;
cout << "How much money do you have?" <> money; cout << "How many packets of milk would you like to buy?" <> quan_milk; cout << "How many jars of coffee would you like to buy?" <> quan_coffee; cout << "How many bars of chocolate would you like to buy?" <> quan_chocolate; cout << "What's price for one bar of chocolate?" <> price_chocolate; cout << "What's price for one jar of coffee?" <> price_coffee; cout << "What's price for one packet of milk?" <> price_milk;
#include “pch.h”
#include
using namespace std;
int main()
{
setlocale(0, “”);
float milk, choc, coffe;
int milkP, chocP, coffeP;
float summ = 0;
cout <> milk;
cout <> milkP;
cout <> choc;
cout <> chocP;
cout <> coffe;
cout <> coffeP;
summ = milk * milkP + choc * chocP + coffe * coffeP;
cout << "Стоимость покупки руб. " << summ << endl;
return 0;
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “rus”);
cout << "Пишем нашу первую программу" << endl;
// creating change
int March = 11;
int cofe = mars + 3;
int milk = mars + cofe; //price of Mars and coffee
int summa = 0;
cout << "обшая сумма затрат-" << sum << endl;
// data display
cout << " the value of chocolates" << mars << endl;
cout << "стоймость кофе" << cofe << endl;
cout << "стоймость балона молока" << milk << endl;
cout << endl; // go one more line
// calculation of the total cost of the rogue
sum = march + cofe + milk;
// display result on screen
cout << " total spent" << sum << "рублей" << endl;
return 0;
}
2.
#include
using namespace std;
int main()
{
int five = 5;
int Eight = five + 3;
int two = five + Eight;
std::cout << " Five: " << five << endl;
std::cout << " Eight: " << Eight << endl;
std::cout << " two: " << two << endl;
return 0;
}
#include
using namespace std;
int main() {
setlocale(LC_ALL< "Rus");
int sp = 6;
int spoon = sp;
int or = sp;
int am = sp + spoon + or;
std::cout << "Всего предметов: " << am << endl;
return 0;
}
Slightly complicated the first task, adding condition.
#include
using namespace std;
int main()
{
int quan_coffee, quan_chocolate, quan_milk;
float price_coffee, price_chocolate, price_milk, money, result_coffee, result_milk, result_chocolate, result_all;
cout << "How much money do you have?" <> money;
cout << "How many packets of milk would you like to buy?" <> quan_milk;
cout << "How many jars of coffee would you like to buy?" <> quan_coffee;
cout << "How many bars of chocolate would you like to buy?" <> quan_chocolate;
cout << "What's price for one bar of chocolate?" <> price_chocolate;
cout << "What's price for one jar of coffee?" <> price_coffee;
cout << "What's price for one packet of milk?" <> price_milk;
result_chocolate = price_chocolate * quan_chocolate;
result_coffee = price_coffee * quan_coffee;
result_milk = price_milk * quan_milk;
result_all = result_chocolate + result_coffee + result_milk;
cout << "You have to pay " << result_all <= result_all) {
float surr;
surr = money – result_all;
cout << "Your surrender is " << surr << "$" << endl;
}
else {
cout << "Sorry, but you don't have enough money" << endl;
}
}
#include
#include
using namespace std;
int main() {
setlocale(0, “RU”);
int Products, sum = 0;
cout <> Products;
string Name[100];
double Price_p[100];
int Count_p[100];
for (int i = 0; i < Products; i ) {
cout <> Name[i];
cout <> Price_p[i];
cout <> Count_p[i];
sum += Price_p[i] * Count_p[i];
}
cout << "\nОбщая стоимость продуктов: ";
for (int i = 0; i < Products; i ) {
cout << Name[i] << ", стоимостью в " << Price_p[i] << " и количеством в " << Count_p[i];
if (++i != Products) {
cout << " + ";
}
else {
cout << " = " << sum;
}
i–;
}
return 0;
}
#include
using namespace std;
int main() {
setlocale(0, “RU”);
cout <> input;
inputpt = input + 3;
fnsp = inputpt + input;
cout << input << " + " << 3 << " = " << inputpt << endl;
cout << input << " + " << inputpt << " = " << fnsp;
return 0;
}
#include
#include
using namespace std;
int main() {
setlocale(0, “RU”);
int cItem = 1, sum = 0;
string nItem;
cout << "Для выхода введите 0." << endl;
while (cItem != 0) {
cout <> nItem >> cItem;
sum += cItem;
}
cout << "Общее количество всех предметов: " << sum;
}