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.

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

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

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

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

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

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

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

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

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

Leave a Reply

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