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. Сделал через цикл for

    int main()
    {
    int c=0; // переменная-счетчик количества введенных чисел
    int a; // переменная для ввода числа
    int b=0; // переменная-счетчик суммы введенных чисел
    for (int i = 0; ;i )
    {
    cout << "Enter number" <> a;
    c = i;
    b += a;

    if (a == 0) // условие выхода из цикла
    {
    break;
    }
    }
    cout << "number of entered numbers "<< c << endl; // выводим количество введенных чисел
    cout << "sum of numbers = " << b << endl; //выводим сумму введенных чисел
    return 0;
    }

  2. 1 задачу решилменее грамотно” =)

    #include “pch.h”
    #include

    using namespace std;
    int main()
    {
    int s = 0; // счетчик чисел
    int sum = 0; // sum
    int x = 1; // для ввода числа

    while (x !=0) // пока х не 0
    {
    cin >> x;
    sum += x;
    s ;
    }
    int z = sum / s;
    cout << "sum: " << sum <<
    "ch: " << s-1 <<" to: " << from;

    return 0;
    }

  3. /*Без оформления
    */
    #include
    using namespace std;
    int main()
    {
    int x[100];int i = 0; int sum = 0;
    while (true) {

    cin >> x[i];

    sum += x[i];
    int ser;
    ser = sum;
    if (x[i] == 0) {
    ser /= i;
    cout << i << endl;
    for (int j = 0; j < i; j ) {
    cout << x[j] << endl;
    }
    cout << sum<< " "<< to be<<endl;
    exit(0);
    }
    i ;
    }

    }

  4. //With min and max
    using System;
    namespace Program
    {
    class Program
    {
    static void Main()
    {
    int counter = 0,
    sumAll = 0,
    userInput,
    averege = 0,
    min = 0,
    max = 0;
    while (true)
    {
    Console.WriteLine(“Please enter a integer : “);
    userInput = Convert.ToInt32(Console.ReadLine());
    Console.Clear();
    if (userInput == 0)
    {
    Console.WriteLine($”\n\nReult : \n\n ” +
    $”Count of integers : {counter}” +
    $”\n Summ of integers : {sumAll} ” +
    $”\n Averege : {averege}” +
    $”\n Maximum : {max} ” +
    $”\n Minumum : {min} “);
    break;
    }
    if(counter == 0)
    {
    min = userInput;
    max = userInput;
    }
    counter++;
    sumAll = userInput + sumAll;
    averege = sumAll / counter;
    min = (min > userInput) ? userInput : min;
    max = (max < userInput) ? userInput : max;
    }
    }
    }
    }

  5. void main()
    {
    setlocale(LC_ALL, “RU”);
    int value = 0;
    int sum = 0;
    int counter = 0;

    while (true)
    {
    cout << "Введите число:" <> value;
    if (value != 0)
    {
    sum += value;
    counter += 1;
    }
    else
    {
    break ;
    }

    }
    cout << "Cумма: " << sum << endl;
    cout << "Счетчик: " << counter << endl;
    }

  6. 3. The task is more difficult. Нарисовать равнобедренный треугольник из символов ^. The height of a user selects. For example: height = 5, on the screen
    Я тут е%№сь 4 часа с циклами while, а у них даже в ответе всё с циклом for.КОНЕЧНО, ТАК ПРОЩЕ, НАХРЕН ПИЛИТЬ ЗАДАНИЕ В ЭТУ ТЕМУ

  7. ————– 1 —————-
    #include
    using namespace std;

    int main() {

    int _input_number_i = 0;
    float _input_number=0;
    float _input_number_arr[100];
    float _input_num_sum=0;

    cout << "Enter the ordinal number, if it will be = 0, the programm will stop entering" <> _input_number;

    while (_input_number) {

    _input_number_arr[_input_number_i] = _input_number;
    _input_num_sum += _input_number;
    cin >> _input_number;
    _input_number_i++;
    }
    if (_input_number_i)
    cout << "There are a " << _input_number_i << " numbers and summa of all numbers is " << _input_num_sum << " ,and the apperanse is " << _input_num_sum / _input_number_i << endl;
    return 0;
    }

  8. ———— 2 ————-
    #include
    using namespace std;

    int main() {
    int _inputNumber = 0;
    int _sumInputNumber = 0;

    cout << "Enter any whole number" <> _inputNumber;
    if (_inputNumber % 2 != 0) {
    _sumInputNumber += _inputNumber;
    cout << "The numbers sum is " << _sumInputNumber << endl;
    }
    }
    return 0;
    }

Leave a Reply

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