The basics of programming in c++ for beginners

Tasks: Data types, variables and constants

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.

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.

3. Declare variables, to count the total number of objects for the table. for example cups, the same number of saucers and spoons.

121 thoughts on “Tasks: Data types, variables and constants

  1. #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;
    }

  2. #include
    using namespace std;

    int main() {
    setlocale(LC_ALL, “rus”);
    int a;
    int b;
    int c;
    int plitka;
    int cofe;
    int moloko;
    cout << "Введите цену (плитка шоколада)" <> plitka;
    cout << "Введите число" <> a;
    cout << "Введите цену (Кофе)" <> cofe;
    cout << "Введите число" <> b;
    cout << "Введите цену (Молоко)" <> moloko;
    cout << "Введите число" <> c;
    int sum = 0;
    sum = a * plitka + b * cofe + moloko * c;
    cout << "Общая цена ";
    cout << sum << endl;
    }

  3. #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;
    //количество упаковок
    cout << "Введите количество пачек шоколада: " <> chocolate;
    cout << "Введите количество пачек молока: " <> milk;
    cout << "Введите количество пачек кофе: " <> coffee;
    //стоимость
    cout << "Введите стоимость пачки шоколада: " <> chocolatecost;
    cout << "Введите стоимость пачки молока: " <> milkcost;
    cout << "Введите стоимость пачки кофе: " <> coffeecost;
    //вычисления
    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;
    }

  4. /* Обьявить переменные с помощью которых можно будет
    посчитать общую сумму покупки нескольких товаров.
    For example a chocolate bar, кофе и пакеты молока */

    #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 << " руб." << endl;
    cout << "Сумма покупки баночки кофе: " << product_coffee << " руб." << endl;
    cout << "Сумма покупки пакеты молока: " << product_milkPackages << " руб." << endl;
    purchase_amount = product_chocolateBar + product_coffee+product_milkPackages;
    cout << endl;
    cout << "Сумма всех покупок: " << purchase_amount << " руб." << endl;
    cout << endl;

    return 0;
    }

  5. // Объявить три переменные типа int и присвоить
    // первой числовое значение, вторая переменная
    // равна первой переменной увеличенной на 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;
    }

  6. // Declare variables, для подсчета общего количества
    // предметов для сервировки стола. 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 – блюдец, c – ложек, d – общее.
    a = 4; b = a; c = b; d=a+b+c;
    cout << "Количесттво чашек: " << a << endl;
    cout << "Количество блюдец: " << b << endl;
    cout << "Количество ложек: " << c << endl;
    cout << "Общее количество предметов " << d << " для сервировки стола!";
    return 0;
    }

  7. Сделал немного по своему, чтобы кол-во продуктов было не ограничено (nearly , насколько хватит типа переменной int).
    #include
    using namespace std;

    int main()
    {
    cout <> productQuantity;
    int x=1; // номер продукта для цикла While + для выведения в консоль
    double z = 0.0; //Переменная для начальной цены
    while (x != (productQuantity+1))
    {
    double y; //Переменная для продукта

    cout << "Please enter the price of the " << x <> Y;
    z = z + Y;
    ++x;

    }
    cout << "Total amount: = " << from;
    }

  8. #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<<" ,Благодарим за покупку!!!"<<endl;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *