The basics of programming in c++ for beginners

Tasks: A for loop in c ++

Once you begin to tasks,  therefore already know what for loop. Let's look at a few of the tasks, solution in which it is applied, and, thereby, strengthen the knowledge gained. Programming practice– the best way to deal with the material and store information for a long time.

1. Write a program, that will show on the screen the number of the square,  entered by the user. The user has to decide for himself –  exit the program or continue writing. (Tip – You must run an infinite loop, which provide for termination of his, upon the occurrence of certain conditions).

2. In the gym every day comes a certain number of visitors. It should prompt the user to enter such data: how many people visited the gym for the day, enter the age of each visitor and ultimately show the age of the oldest and the youngest of them, as well as to calculate the average age of visitors.

For the job yourself, We offer you to solve a similar task. Arrange the number of visitors entering the gym and the number of hours spent by each of them in the gym. As a result, calculate and display the total amount, which customers have paid for training.

 3. In stock has a certain number of boxes of apples (in this example, 15). When the car arrives for pickup, ask the user to enter, how many boxes loaded into the first car,  the second and so on, until there are no more boxes of apples. Provide case, When the user enters the number of boxes more, than there is in stock.

If you have questions, please contact us in the comments.

161 thoughts on “Tasks: A for loop in c ++

  1. #include
    #include // To use the setw()

    int main() {
    int box = 15;
    int out = 0;

    std::cout << "Now we have " << box << " boxes\n";
    std::cout << "————————\n";
    std::cout << "Truck\t| Boxes Loaded\n";
    std::cout << "————————\n";

    for (int i = 1;; i ) {
    std::cout << i <> out;

    if (out > box) {
    std::cout << "We have only " << box << " boxes\n";
    continue;
    }

    box -= out;
    std::cout << "We have " << box << " boxes left\n";

    if (box == 0)
    break;
    }

    std::cout << "————————\n";

    return 0;
    }

  2. #include
    #include

    int main() {
    int box = 15;
    int out = 0;

    std::cout << "Now we have " << box << " boxes\n";
    std::cout << "————————\n";
    std::cout << "Truck\t| Boxes Loaded\n";
    std::cout << "————————\n";

    for (int i = 1;; i ) {
    std::cout << i <> out;

    if (out > box) {
    std::cout << "We have only " << box << " boxes\n";
    continue;
    }

    box -= out;
    std::cout << "We have " << box << " boxes left\n";

    if (box == 0)
    break;
    }

    std::cout << "————————\n";

    return 0;
    }

  3. #include
    using namespace std;
    void main()
    {
    setlocale(LC_ALL, “Rus”);
    int square;

    for ( ; ;)
    {
    cout << "Введите число для получения его значения в квадрате (0 = exit)" <> square;
    if (square == 0)
    {
    cout << " Конец программы";
    break;
    }
    cout << "Выше число в квадрате =" << square * square << endl;
    }
    }

  4. an easier option
    #include
    using namespace std;
    int main() {
    setlocale(LC_ALL, “RU”);
    int box = 15;
    int a;
    int i = box;
    int x = 1;
    while ( i > 0)
    {
    cout << "Сколько ящиков кинуть "<<x<> a;
    i-= a;
    x++;
    if (i == 0)
    cout << "Ящики закончилсь \n";
    else if (i < 0) {
    cout << "Вы ввели неправильное значение ящиков у вас осталось: \n";
    i += a;
    cout << i<<endl;
    x–;
    }
    }
    return 0;
    }

  5. #include Self-Assignment Solution(rate from 1 to 10)
    using namespace std;
    int main(){
    int hum;//number of visitors per day
    int time_1 = 5000;//amount for 1 hour of training
    int summa_1;//total amount, paid by all visitors
    cout <> hum;
    int times[hum];
    int summa = 0;//total hours
    for(short time = 0; time < hum; time++){
    cout << "Сколько часов провёл " << time + 1 <> times[time];
    summa += times[time];
    }
    cout << "Общая сумма часов: "<< sum << "ч."<< endl;

    amount_1 = amount*time_1;
    cout <<"Общая стоимость: " << summa_1 << "руб." << endl;

    return 0;
    }

  6. 3)Rumor mb navnokodyl, but he himself!
    int box = 15;
    std::cout << "Всего яблок – " << box << '\n';
    for (int i = 1; i <= 15; ++i) {
    int a, b;
    std::cout << "В какую машину хотите закинуть яблоки?\n 1.Redn 2. Green" <> a;
    if (a == 1) {
    std::cout <> b;
    box-=b;
    system(“cls”);
    }
    if (a == 2) {
    std::cout <> b;
    box-=b;
    system(“cls”);
    }
    std::cout << "\nЯблок осталось – " << box << '\n';
    if (box == 0) {
    system("cls");
    break;
    }
    }
    std::cout << "Яблоки кончились…";

  7. Good?

    #include
    using namespace std;

    int main() {
    setlocale(LC_ALL, “RU”);

    int box = 15;
    int forSale = 0;
    for (int i = 1; box > 0; i ) {
    cout << "Сейчас на складе " << box << " boxes of apples" << endl;
    cout << "Введите кол-во коробок для погрузки в " << i <> forSale;
    if (forSale > box) {
    for (;forSale > box;) {
    cout << "На складе нет столько коробок" <> forSale;
    }
    }
    box -= forSale;
    }
    cout << "Все коробки были погружены" << endl;

    return 0;
    }

      1. chzh

        #include
        using namespace std;

        int main() {
        setlocale(LC_ALL, “RU”);

        int box = 15;
        int forSale = 0;
        for (int i = 1; box > 0; i ) {
        cout << "Сейчас на складе " << box << " boxes of apples" << endl;
        cout << "Введите кол-во коробок для погрузки в " << i <> forSale;
        if (forSale > box) {
        for (;forSale > box;) {
        cout << "На складе нет столько коробок" <> forSale;
        }
        }
        box -= forSale;
        }
        cout << "Все коробки были погружены" << endl;

        return 0;
        }

  8. // A task 2 ANSWER
    #include
    int main()
    {
    using namespace std;

    cout <> max;
    int* p = new int[max];
    double srnum{};
    for (int i{}; i < max; i )
    {
    cout << "Enter age " << i + 1 <> *(p + i);
    srnum += (*(p + i) / max);
    }
    int tempMax = *p;
    int tempMin = *p;

    for (int i = 1; i tempMax)
    tempMax = *(p + i);
    if (*(p+i)<tempMin)
    tempMin = *(p + i);
    }
    cout << "Maximum age: " << tempMax << endl;
    cout << "Minimum age: " << tempMin << endl;

    cout << "Average age: " << srnum;
    }

Leave a Reply

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