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.

130 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 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;
    }

  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;
    //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;
    }

  4. /* 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;
    }

  5. // 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;
    }

  6. // 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;
    }

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

  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<<" ,Thank you for your purchase!!!"<<endl;
    }

Leave a Reply

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