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. you can use any name for the counter. You can also use a variable declared before the loop. It depends on what you need to implement the code.
      Just do not complicate the code, inventing their own unique name for the usual standard things. For the name of the loop counter i – most that, that we should. This reduction of the index or iterator. Perhaps by indicator (counter, флаг).

  1. a task 2, I got such a decision:

    #include
    #include
    using namespace std;

    int main(){
    int visitors, age, older, younger, average;

    cout <<visitors;

    int count[visitors];
    for(int i=1; i<=visitors; i++) {
    cout <<"Enter the age of " <<i <>count[i-1];
    }
    older = younger = average = count[0];

    for(int i=1; i<visitors; i++) {
    if(count[i]older) older = count[i];
    average += count[i];
    }
    cout <<"\n\nThe younger visitor is " <<younger <<" years old\n";
    cout <<"The older visitor is " <<older <<" years old\n";
    cout <<"The average age of visitor is: " <<average/visitors <<endl <<endl;

    system("pause");
    return 0;
    }

  2. #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    int kol_vo = 0;
    int vozrast = 0;
    int maxAge = 0;
    int minAge = 100;

    int sum = 0;
    cout << kol_vo;
    cout << "\n";
    for (int i = 0; i < kol_vo; i++)
    {
    cout <> vozrast;
    if (vozrast > maxAge) // если оно больше, чем хранит переменная max
    maxAge = vozrast; // записываем в неё это число
    if (vozrast < minAge)
    minAge = vozrast;
    sum = sum + vozrast;
    }
    cout << "Возраст самого старшего = " << maxAge << "\n";
    cout << "Возраст самого младшего = " << minAge << "\n";
    cout << "Средний возраст посетителей = "<<sum/kol_vo;
    cin.get();
    }

  3. In the code of the first tasks of the uncertainty regarding the exit from the program. If a user wants to click “Y”, to continue, but accidentally clicks promahnotsya and any other symbol, Besides “N” – program anyway completed.

  4. #include
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "rus");
    long long x = 0;
    cout <> x;
    if (x == 0)
    break;

    else
    {
    x = x * x;
    cout << x << endl;

    }

    }
    _getch();
    return 0;
    }

Leave a Reply

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