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. If anyone is suddenly interested, an alternative solution to the second problem using a for loop. :)

    #include
    using namespace std;

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

    int debut;
    int fin;
    int number;
    int sum = 0;

    cout << debut;
    cout << fin;

    for(int i = (debut - 1); i < fin; ++i){
    number = debut;
    if (number % 2 != 0){
    sum = sum + number;
    debut = debut + 1;
    }
    else{
    debut = debut + 1;
    continue;
    }
    }
    cout << "Сумма нечётных чисел в данном диапазоне: " << sum << endl;

    }

  2. I’m the only one who doesn’t understand the working principle of whille??? working program,but not sure,what do while is needed. and especially for “upstarts” input “fool's protection”
    case 9:
    {
    /*. Необходимо суммировать все нечётные целые числа в диапазоне,
    который введёт пользователь с клавиатуры*/

    double a = 0;
    double b = 0;
    int i = 0;
    double s = 0;

    cout << a;
    cin >> b;
    do
    {
    if (a < b)
    {
    for (i = a; i <= b; i++)
    {
    if (i % 2 == 0)
    {
    continue;
    }
    s += i;
    cout << "нечетные числа из вашего диапазона\t" << i << endl;

    }cout << "сумма этих чисел = \t" << s < b)
    {
    for (i = b; i <= a; i++)
    {
    if (i % 2 == 0)
    {
    continue;
    }
    s += i;
    cout << "нечетные числа из вашего диапазона\t" << i << endl;
    }
    cout << "сумма этих чисел = \t" << s << endl;
    }

    break;
    } while (a == b);
    cout << "алльенЬ\n";
    break; }

  3. int main()
    {
    setlocale(LC_ALL, "rus");

    int a = 0, b = 0, i = -1;
    do
    {
    a = _getche() - 48;
    b = a + b;
    i++;
    } while (a != 0);
    if (b == 0)
    exit(0);
    cout << "\nСумма = " << b << endl
    << "Колличество введеных знаков = " << i << endl
    << "Среднее арефметическое = " << static_cast(b) / i << endl;

    system("pause");
    return 0;
    }

  4. третья

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

    cout << h;
    do
    {
    if(h!=1)
    cout << setfill(' ') << setw(h-1) << ' '
    << setfill('^') << setw(k) << '^' << endl;
    else
    {
    cout << setfill('^') << setw(k) << '^' << endl;
    }
    --h; k += 2;
    } while (h);

    system("pause");
    return 0;
    }

  5. Regarding the third task.
    more or less figured out the cycles, which set the height of the shape and the number of spaces.
    But for the life of me I can’t figure out the cycle, which outputs the symbol, a for (int j = height – 2 * i; j <= height; j ).
    Please who- someone explain why the variable j is initialized to this particular value. Thanks in advance!

    1. int main()
      {
      setlocale(LC_ALL, "rus");

      int height = 0; // высота треугольника

      cout << height;

      for (int i = 0; i = i; j--)
      {
      cout << "!";
      }

      for (int j =0; j <= i; j++)
      {
      cout << '^';
      }
      for (int j=1; j<= i; j++)
      {
      cout <<'^';
      }
      cout << endl;
      }
      return 0;
      }

      1. They forgot to mention in the problem statement, that the symbol output operators, spaces and line breaks can only be used once…that's when it gets interesting ;)

  6. “Tasks: while loops, do while, nested loops”
    It is not clear why the third problem was solved using for(;;), doesn't this contradict the topic??
    And how to frame the code?)

    1. The for loop, while, do while – these are different forms the same thing – each of them can be rewritten through any other.
      So there's not much difference.

      1. [quote]The for loop, while, do while are different forms of the same thing - each of them can be rewritten in terms of any other.
        So there's not much difference.[/quote]
        I understand this perfectly,however for for loops() there is a separate topic. That is. logically, or collect all the cycles into one topic, or pay attention to the disclosure of the announced topic.

    2. how to frame the code?
      This is **text markup** by inserting BBCode tags.
      In order for the, to mark up **program code** you need to write something like this (only instead of square brackets [ ] write angular ), like this:
      [code]
      … this is where you write your code …
      [/code]
      (Do a search on BBCode for a more detailed explanation.)

  7. [code with ++]
    int main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int a, b;
    int sum = 0;

    cout << "Введите диапазон чисел" << endl;
    cout <> a;
    cout <> b;

    cout << "Сумма не четных чисел: " << endl;
    while (a != b + 1) {
    if (a % 2 != 0) {
    sum += a;
    if (a == b – 1 || a == b) {
    cout << a;
    }
    else cout << a << " + ";
    }
    a++;
    }
    cout << " = " << sum << endl;

    return 0;
    }
    [/code]

  8. [code]
    int main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int a, b;
    int sum = 0;

    cout << "Введите диапазон чисел" << endl;
    cout <> a;
    cout <> b;

    cout << "Сумма не четных чисел: " << endl;
    while (a != b + 1) {
    if (a % 2 != 0) {
    sum += a;
    if (a == b – 1 || a == b) {
    cout << a;
    }
    else cout << a << " + ";
    }
    a++;
    }
    cout << " = " << sum << endl;

    return 0;
    }
    [/code]

Leave a Reply

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