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

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

    1. And in this form the task is practically not complicated. … but requires a fair amount of ingenuity at the production level, because the standard deviation must be calculated in the same single cycle, as is the accumulation of the amount, and when the average is still unknown. Because when the sequence is counted, for example, 10000 values (which is quite common in real experiments) no one will allow you to store this lot of values ​​for a repeated loop.

  1. Ahahaha, and I did the first thing

    #include
    #include
    using namespace std;

    int main()
    {
    int i = 1;
    int sum = 0;
    while (i != 0) {
    cout << "Enter number. If you want to stop, type 0: " <> i;
    sum = sum + i;
    }
    cout << "And the sum of your numbers is ..." << sum << " !" << endl;
    system("pause");
    return 0;
    }

  2. My solution to the second problem

    #include
    using namespace std;

    int main (){

    setlocale (LC_ALL, "rus");

    //Переменные
    int iMinValue(0); // Минимальное значение диапозона
    int iMaxValue(0); // Максимальное значение диапозона
    int iSumValue(0); // Сумма нечетных чисел в диапозоне

    //Запрос на ввод диапозона чисел
    cout << iMinValue;
    cout << iMaxValue;
    cout << "Нечетные числа в диапозоне от " << iMinValue << " до " << iMaxValue << ": ";

    // Выводим все нечетные числа диапозона
    for (int i = iMinValue; i < iMaxValue + 1; i++)
    {
    if (i % 2 != 0) // Если остаток от деления не равен 0, то выводим число в ряд
    {
    cout << i << " "; // Выводим ряд нечетных чисел из диапозона
    iSumValue += i; // Записываем сумму нечетных чисел
    }
    }
    cout << endl;
    cout << "Сумма нечетных чисел: ";
    cout << iSumValue;
    cout << endl;
    }

  3. Guys, please tell me for the third task how to finish the stump to the pyramid so that it turns out to be a yolka

    1. Reply from Stilet:
      Write in thread:
      at:
      setlocale(LC_ALL, “C”);
      for (int i = 0; i < height/2+3; i++){ cout.width(height/2+3); for (int j = 1; j < 6; j++) cout<<(char)2510; cout << endl; }

  4. I understand so, no one solved the third problem on their own, and the admin doesn’t care about requests to comment on the decision.

    1. Don't mock me… there's only 12 lines of code with loops, who puzzled you. I'll do you a big favor, if I don't comment, and you will carefully analyze this code line by line and understand, what's what.
      Let's, the height of the triangle is 4 (height = 4). Just imagine, what happens to this variable in loops (go through them mentally step by step ). And that, that the first inner loop draws spaces, and the second characters – it's obvious.

Leave a Reply

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