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
    using namespace std;
    int main()
    {
    setlocale(LC_ALL,”Rus”);
    int user = 0;
    int hour = 0;
    int sumHour = 0;
    float cost = 55.5;
    char button = ‘y’;

    for (;;)
    {
    cout <> user;
    for (int i = 0; i < user; i )
    {
    cout << "Сколько часов занимался " << i + 1 << "-й посетитель? " <> hour;
    sumHour += hour;

    }
    float prise = sumHour * cost;
    cout << "Общая сумма == " << taking << " USD\n";
    cout <> button;
    if (button != 'Y’ && button != 'And');
    break;
    }
    system(“pause”);
    return 0;

  2. Ruga not for oformlenye
    #include
    using namespace std;
    int main()
    {
    int box= 15;
    int minusBox[15];
    cout << "4islo boxiv:15";
    cout << endl;
    for (int i = 0; i < 15; i ) {

    cout << "y " << (i+ 1) <> minusBox[i];
    box-=minusBox[i];
    if (box == 0) {
    cout << "zakin4ilisya box";
    exit(1);
    }
    else if (box < 0) {
    cout << "Hemae stilki boxiv";
    exit(0);
    }
    cout << "lischilosya boxiv:" << box << endl;

    }

    }

  3. #include
    #include

    using namespace std;

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

    int visitors;
    int time;
    int sum;

    cout << "Введите кол-во посетителей " <> visitors;

    for (int i = 0; i<visitors; i )
    {
    cout << "Введите кол-во часов проведнное в спортзале " <> time;
    sum = time*100;
    cout << " Общая сумма "<< sum << "$" << endl;

    }

    return 0;
    }

  4. in their little did))
    #include

    using namespace std;

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

    int chel, let, c, min, max, srar, chas, deng, dengivchas,chas2;
    cout << "Введите количество человек прошедших в спорт зал" <> chel;
    cout << "Введите стоимость посещения спорт зала в час" <> dengivchas;
    min = 0;
    max = 0;
    c = 0;
    deng = 0;
    chas2 = 0;
    for (int i = 1; i <= chel; i ) {
    cout << "Сколько лет " << i << " person?" <> let;
    cout << "Сколько часов провёл в спорт зале?" <> chas;
    if (i == 1)
    min = slight;
    if (max = let)
    min = slight;
    c += let;
    chas2 + = chas;
    }
    deng = chas2 * dengivchas;
    srar = c / chel;
    cout << min << " Это минимальный возраст"<<endl << endl;
    cout << max << " Это максимальный возраст"<<endl << endl;
    cout << srar << " Average age" << endl << endl;
    cout << deng << " Profit" << endl << endl;
    return 0;
    }

  5. #include

    using namespace std;

    int main()
    {
    int numBoxes = 15;
    int downloadBoxes = 0;

    for(int i;;i )
    {
    cout<<"Введите, сколько ящиков с яблоками вы хотите загрузить в "<< i+1 <> downloadBoxes;

    if(downloadBoxes > numBoxes)
    {
    cout<<"Ящиков в наличии: "<< numBoxes<<endl;
    i–;
    }
    else
    {
    numBoxes -= downloadBoxes;
    }

    if(numBoxes == 0)
    {
    cout<<"Ящики с яблоками закончились! Давай до свидания!";
    break;
    }

    }

    return 0;
    }

  6. #include

    using namespace std;

    int main()
    {

    int guests = 0;
    int hourGuests = 0;
    int summ = 0;
    int totalSumm = 0;

    cout <> guests;
    cout << "Введите кол-во часов каждого посетителя: ";
    for(int i = 0; i > hourGuests;
    summ + = hourGuests;
    }
    totalSumm = summ * 10;
    cout << "Общая сумма которую посетители заплатили за тренировку в спортзале: " << totalSumm << " $ "<< endl;

    return 0;
    }

      1. #include

        using namespace std;

        int main()
        {

        int guests = 0;
        int hourGuests = 0;
        int summ = 0;
        int totalSumm = 0;

        cout <> guests;
        cout << "Введите кол-во часов каждого посетителя: ";
        for(int i = 0; i > hourGuests;
        summ + = hourGuests;
        }
        totalSumm = summ * 10;
        cout << "Общая сумма которую посетители заплатили за тренировку в спортзале: " << totalSumm << " $ "<< endl;

        return 0;
        }

  7. JOB №3
    (I did)

    #include
    using namespace std;

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

    int c;

    cout << "введите количество ящиков на складе :" <> c;

    cout << "__________________________________________" << endl << endl;

    int BOX = c;
    int a;
    int b=0;

    cout << "подьехала машина для погрузки" << endl << endl;

    cout << "__________________________________________" << endl << endl;

    for ( int i = 0; i < BOX; i )
    {
    cout << "осталось ящиков на складе :" << BOX << endl << endl;
    cout << "__________________________________________" << endl << endl;
    b += 1;
    cout << "сколько ящиков загрузить в " << b << "-ую " << "машину :" << endl <> a;

    if (a > BOX)
    {

    cout << "на складе ящиков меньше чем :" << a << endl << endl;

    cout << "__________________________________________" << endl << endl;

    cout << "введите другое количесво ящиков :" << endl <> a;

    cout << "__________________________________________" << endl << endl;

    BOX -= a;

    cout << "__________________________________________" << endl << endl;

    }

    else {

    BOX -= a;

    cout << "__________________________________________" << endl << endl;
    }

    }

    cout << "cклад ящиков опустошен" << endl << endl;

    }

  8. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “Russian”);

    int n;
    cout <> n;
    cout << "На складе есть " << n <<" boxes of apples!" << endl;
    int Kolichestvo,number_car=1;
    for (int i = 0; i < n; )
    {
    cout << "Введите количество ящиков, which is immersed in " <<number_car<> Kolichestvo;
    if (Kolichestvo <= n-i)
    {
    i + = Kolichestvo;
    number_car++;
    }
    else
    cout << "Вы ввели больше ящиков чем на складе" << endl;
    cout <<"*************************************"<<endl;
    }
    cout << "Машины в пути:)";

    }

Leave a Reply

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