The basics of programming in c++ for beginners

Tasks: while loops, do while, nested loops

We met with loops while and do while in C++ and c nested constructions in loops. Let us tasks solving.

1. Organize continuous input of numbers with the keyboard, until the user has entered 0. After entering a zero, show on the screen the number of numbers, which were introduced, their total amount and the arithmetic mean. Tip: you must declare the counter variable, that will count the number of entered numbers, and variable, that will accumulate a total sum of numbers.

2. It is necessary to sum up all the odd integers, which will introduce the user to the keyboard.

3. The task is more difficult. Draw an isosceles triangle of characters ^. The height of a user selects. For example: height = 5, on the screen

task nested loops

174 thoughts on “Tasks: while loops, do while, nested loops


  1. int main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int height;
    int a, b;

    cout <> height;

    a = b = height;

    for (int i = 0; i < height; i++) {
    for (int j = 0; j = a && j <= b) cout << "^";
    else cout << " ";
    }
    a--;
    b++;
    cout << endl;
    }

    return 0;
    }


  2. #include

    using namespace std;

    int main()
    {
    setlocale(LC_ALL, "ru");

    int min_digit = 0;
    int max_digit = 0;
    int total_digit = 0;

    cout << "Введите диапзон!" << endl;
    cout <> min_digit;
    cout <> max_digit;

    while (min_digit > max_digit)
    {
    cout << "Минимальное значение не может быть больше максимального!" <> max_digit;
    }

    for (int i = 0; i != (max_digit - min_digit); i++)
    {
    if ((min_digit + i) % 2)
    {
    cout << min_digit + i << endl;
    total_digit += (min_digit + i);
    }
    }

    cout << total_digit << endl;

    return 0;
    }

    Fine?

  3. #include “stdafx.h”
    #include
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
    setlocale(LC_ALL, “rus”);
    int a = 1, count = -1, sum = 0, av = 0;
    while (a!=0)
    {
    cout <> a;
    sum += a;
    ++ account;
    }
    of = sum / account;
    cout << "Сумма чисел : " << sum << endl;
    cout << "Количество чисел : " << account << endl;
    cout << "Среднее арифметическое : " << of << endl;
    system ("pause");
    }

  4. The latter problem is pretty simple. It can be implemented in C and:

  5. When I studied the topic, started tasks, I begin to solve, something about estimated, compiled, Did not work out, I take into account the error, edited ..
    and so several times, until you get the right solution. This procedure is correct? Or I should initially consider all job, and then start writing program. I am learning from scratch, in principle, all tasks are solved itself, but, sometimes, 1-2 hours spent on each. This is normal, or I'm hopeless?)

    1. This procedure is correct – programs always work out the method of successive approximations.

      Although consider in advance the order follow-up is also useful: then the number will be less than these approximations.

  6. 3. Обчислити значення кореня кубічного з точністю , використавши ітераційну формулу Ньютона . Підрахувати кількість ітерацій, за які досягається бажана точність. Точність обчислення визна¬ча-ється як модуль різниці двох послідовних ітерацій.

  7. А есть ли код для нахождения максимального числа из введенных(1 a task)?
    Если есть, prompt, you are welcome

  8. ИМХО Задачу 1 через цикл for(;;) легче сделать, а через while как-то не очень понятно.

    1. Наконец-то дошло как через while сделать:

    2. Во многих публикациях и разных языках программирования цикл while считается более общей формой, чем цикл for.

Leave a Reply

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