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() {

    int number = 0;
    bool is_active = true;

    while (is_active){
    cout << "enter the nuber" <> number;
    cout << "Mutqagrvac " << number << " tvi qarakusin = " << number * number << endl << endl;
    }
    return 0;
    }

  2. //////////////////////////////3/////////////////////////////////
    #include
    using namespace std;

    int main() {
    int _yashikneri_tiv = 15;
    int _num_of_box;

    cout << "Enter the number of boxis to enter in the car:" <> _num_of_box;
    if (_num_of_box > _yashikneri_tiv) {
    cout << "The number of entered boxis is more then in the stock !!!" << endl;
    }
    else {
    _yashikneri_tiv-= _num_of_box;
    }
    }

    return 0;
    }

  3. #include

    using namespace std;

    int main() {
    setlocale(0, “RU”);
    int Boxes, bfput;
    cout <> Boxes;
    for (int i = 1;; i ) {
    cout << "Сколько ящиков погрузить в " << i <> bfput;
    Boxes -= bfput;
    if (Boxes < 0) {
    Boxes += bfput;
    cout << "Вы не можете погрузить столько ящиков. Boxes left: " << Boxes << " pcs(and)" << endl;
    i–;
    }
    else {
    cout << "Ящиков осталось: " << Boxes << endl;
    }
    if (Boxes == 0) {
    cout << "Яблоки закончились! Гуд бай";
    break;
    }
    }
    return 0;
    }

  4. #include

    using namespace std;

    int main() {
    setlocale(0, “RU”);
    int input;
    for (;;) {
    cout <> input;
    if (input != 0) {
    cout << input * input << endl;
    }
    else {
    cout << "Выход…";
    break;
    }
    }
    return 0;
    }
    But it would be easier to do through while or do-while

  5. #include

    using namespace std;

    int main() {
    setlocale(0, “RU”);
    int countVisitors, maxAge = 0, minAge = 1000, ageVisitor, sum = 0;
    cout <> countVisitors;
    for (int i = 1; i <= countVisitors; i ) {
    cout << "Введите возраст " << i <> ageVisitor;
    if (ageVisitor > maxAge) {
    maxAge = ageVisitor;
    }
    if (ageVisitor < minAge) {
    minAge = ageVisitor;
    }
    sum + = ageVisitor;
    }
    cout << "Самый большой возраст: " << maxAge << endl;
    cout << "Самый маленький возраст: " << minAge << endl;
    cout << "Средний возраст: " << sum / countVisitors;
    }

  6. #include
    using namespace std;

    int main() {
    setlocale(LC_ALL, “RUS”);
    float apple, mash, trash;
    cout <> apple;
    for (int i = 1; apple > 0; i ) {
    cout << "Сколько яблок забрать машиной номер " << i <> trash;
    if (trash <= apple) {
    apple = apple – trash;
    }
    else {
    cout << "недостаточно яблок на складе" << endl;
    i–;
    }
    }
    cout << "Весь груз забран!" << endl;
    }

    1. #include
      #include
      using namespace std;
      int main() {
      SetConsoleCP(1251);
      SetConsoleOutputCP(1251);
      int box = 15;
      int num;
      int sum = 0;
      for (int i = 0;; i )
      {
      cout << "Сколько ящиков загрузить в " << i + 1 <> on one;
      sum + = num;
      if (sum >= 15) {
      cout << "\n" << sum – 15 << " ящиков осталось не загружено ";
      break;
      }
      }
      return 0;
      }

  7. Help me figure out the problem about the gym, please. I don’t understand how we will see the maximum and minimum age on the screen.. For example, we write that the age of the 1st visitor 25. First if check in progress (age>maxage). i.e.. 25 more O returns true. Then a second check of the if condition (age<minage) and also returns truе. So how does the program determine the min and max age if it enters the same data into age??

    1. #include
      using namespace std;
      int main()
      {
      setlocale(LC_ALL, “Russian”);
      int yabloki,a;
      cout <> yabloki;
      cout << "Подъехали машины для загрузки\n " << "……………………\n"<<"Загрузите ящики с яблоками… \n\n";
      for (int i = 1; ; i ) {
      cout << "Сколько ящиков загрузить в " << i <> a;
      if (a <= yabloki) {
      cout << "Вы загрузили " << a << " box(_) \n";
      yabloki = yabloki – a;
      cout << "Осталось " << yabloki << " box(_) \n\n";
      if (yabloki == 0) {
      cout << "………………\n";
      cout << "Поздравляю,you have loaded all the boxes!!!\n\n";
      break;
      }
      }
      else {
      cout << "……………………………….\n";
      cout << "Вы ввели превышающее количество яблок ";
      break;
      }
      }

      }

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

    int Box = 15; // number of boxes in stock
    int BoxLoad = 0; //number of loaded boxes
    int BoxVar; //variable for counting the remaining boxes

    cout << "На складе " << Box << " boxes of apples" << endl;

    for (int i = 1;; i )
    {

    cout << "Введите количество ящиков для отгрузки в " <<i<<" машину"<> BoxLoad;

    if (BoxLoad<=Box)
    {
    Box -= BoxLoad;
    cout << " The rest of the boxes with apples in the warehouse " << Box << " PC." << endl;

    }
    else
    {
    BoxVar = Box + BoxLoad;
    cout << "На складе нет столько ящиков. Boxes in stock: "<< Box << endl;
    i–;
    }
    if (Box==0)
    {
    cout << "Ящиков не осталось" << endl;
    break;
    }
    }

Leave a Reply

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