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.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Tasks: A for loop in c ++
4.3 (85.45%) 11 votes

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

  1. I have something like that happen

    1. после bool exit; and to cycle these two lines. probably something accidentally erased

      1. some bugs. twice is not poulchaetsya send code
        shorter after Boolean variable until cycle and these two lines:
        cout <> appleWarehouse;

    1. the first line is the output, which asks how many apples is in stock,and the second line is putting these very apples. why the code site does not eat – I do not know

  2. #include
    using namespace std;

    int main()

    {
    while (1)
    {
    setlocale(LC_ALL, “rus”);
    int i = 0; // declaration and initialization of the control variable
    char j = ‘Y’;
    int c = 0;
    cout << "\nВыберите программу\n";
    cout <> c;
    if (c == 1)
    {
    for (;;) // запуск бесконечного цикла
    {
    cout <> i;
    cout << "\nЧисло в квадрате=" << and * and << endl;
    cout <> j;
    if (j != 'Y’ && j != 'And')
    break;
    }
    }
    else if (c == 2)
    {
    for (;;) // запуск бесконечного цикла
    {
    int a;
    cout <> i;
    cout <> a;
    if (a == i*i)
    cout << "\nОтвет верен\n";
    else cout << "\nОтвет не верен\nПравильный ответ=" << and * and;
    cout <> j;
    if (j != 'Y’ && j != 'And')
    break;
    }
    }
    else
    return 0;
    }
    }

  3. Решение 1й задачи, используя цикл while

    #include
    #include
    using namespace std;

    /* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

    int main(int argc, char** argv) {

    cout << "type 1337 for end programm." << endl;
    int exit = 1337;
    int a;
    int sqrt;
    cout <> a;

    while (a!=exit)
    {
    sqrt = a*a;
    cout << a << " * " << a << " = " << sqrt << endl;
    cout <> a;
    }

    return 0;
    }

  4. file:///C:/Users/Romul/Documents/C++%20%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8/%D0%A6%D0%B8%D0%BA%D0%BB%D0%B8/%D0%B2%D0%BF%D1%80%D0%B0%D0%B2%D0%B03/main.html
    как-то так

    1. #include
      using namespace std;

      int main()
      {
      setlocale (0, “ukr”);
      int quantity = 20, numberOfCar, packing;
      cout<<"На складi 20 ящикiв"<<endl;
      cout<<"Ваша задача розподiлити їх по грузовим машинам(можна не порiвну)"<<endl;
      cout<<endl;
      cout<<endl;
      cout<>numberOfCar;
      cout< 20 && numberOfCar < 1)
      {
      cout<<"Error";
      break;
      }
      else
      {
      for (int a=0; a < numberOfCar; a++)
      {
      cout<>packing;
      cout<<endl;
      if (quantity quantity) cout<<"Погрузка не здiйснена, перевiрте правильнiсть вводу данних та к-сть ящикiв на складi"<<endl; //packingрозфасовка
      quantity-=packing;
      }
      }
      break;
      }
      return 0;
      }

  5. #include
    #include
    #include

    using std::cout;
    using std::cin;
    using std::endl;

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

    int b = 15;
    int c;

    cout << "Количество ящиков на складе: " << b << endl < 0; i – c)
    {
    if (b > 0)
    {
    cout << "Подъехала машина. Сколько ящиков в неё погрузить?" <> c;
    if (b – c >= 0)
    {
    cout << "Остаток ящиков на складе " << b – c << endl << endl;
    b = b – c;
    }
    else if (b – c < 0)
    cout << "Количество ящиков на складе: " << b << ". Введите меньшее количество." << endl << endl;
    }
    if (b == 0)
    {
    cout << "Склад пуст.";
    break;
    }
    }

    _getch();
    return 0;
    }

  6. Вот можно же в цикле for оставить пустые скобки (;;)? А если пропустить только условие повторения? Почему у меня не инкрементируется i? Постоянно выдаёт “2 в степени 2 = 4″(разумеется если вводить 2).

    #include
    #include
    #include

    using std::cout;
    using std::cin;
    using std::endl;

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

    int a;
    int c;

    cout <> a;

    for (int i = 2;; i )
    {
    int r (0);

    if (i = 2)
    r = a * a;
    if (i != 2)
    r = r * a;

    cout << a << " в степени " << i << " = " << r << endl;
    cout <> c;
    if (c == 0)
    break;
    i ;
    }

    _getch();
    return 0;
    }

  7. Копирую из исходника, а сохраняется в комменте
    cout a;”,
    instead
    “cout <> a;”

Leave a Reply

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