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

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

  1. Hi!
    Here is my simplified version. I copied the wrong top)

    int main()
    {
    int height;
    cout <> height;
    for (int i = 1; i <= height; i )
    {
    for (int j = 1; j = height – i + 1) && (j <= height + i – 1)) ? cout << "^" : cout << " ";
    }
    cout << endl;
    }
    return 0;
    }

  2. Write a program, which print a body depending on the time, if it falls from a height H from the initial skorostyuV0. Count the number of time points, when the current altitude mensheH / 2.

  3. Если кому интересно разобраться в альтернативе:

    #include
    using namespace std;

    int main()
    {
    int height = 0;
    cout <> height;

    for (int i = 0; i < height ; i )
    {
    for (int j = 0; j < height + i; j )
    {
    if (j < height – i – 1)
    cout << " ";
    else
    cout << "^";
    }
    cout << endl;
    }

    system ("pause");
    return 0;
    }

  4. #include
    using namespace std;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    int num, sum = 0, i = 0;
    while (on one != 0)
    {
    for (i;;++i)
    {
    cout <> on one;
    sum + = num;
    ++i;
    }
    }
    cout << "Общая сумма введенных чисел " << sum;
    cout << "\nКоличество чисел " << i << endl;
    cout << "Среднее арифметическое " << sum / i << endl;
    return 0;
    }

  5. using namespace std;
    int num1, num2, sum;
    int main()
    {
    setlocale(LC_ALL, “rus”);
    do
    {
    cout <> num1;
    cout <> num2;
    } while (num1 > num2);
    for (num1; num1 <= num2; ++num1)
    {
    if (num1 % 2 != 0)
    {
    sum += num1;
    }
    }
    cout << "\nСумма нечетных чисел равна = " << sum << endl;
    return 0;
    }

  6. вот еще вариант задачи с треугольником
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);
    int a;
    int i=0, j=0;
    cout <> a;

    for(i=0;i<a;i )
    {
    for(j=1;j= a – i && j <= a + i) cout << "^";
    else cout << " ";

    }
    cout<<endl;
    }

    return 0;
    }

Leave a Reply

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