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. I did it through a for loop

    int main()
    {
    int c=0; // variable-counter of the number of entered numbers
    int a; // variable for entering a number
    int b=0; // variable-counter of the sum of entered numbers
    for (int i = 0; ;i )
    {
    cout << "Enter number" <> a;
    c = i;
    b += a;

    if (a == 0) // loop exit condition
    {
    break;
    }
    }
    cout << "number of entered numbers "<< c << endl; // display the number of entered numbers
    cout << "sum of numbers = " << b << endl; //display the sum of the entered numbers
    return 0;
    }

  2. 1 solved the problem “less competent” =)

    #include “pch.h”
    #include

    using namespace std;
    int main()
    {
    int s = 0; // number counter
    int sum = 0; // sum
    int x = 1; // to enter a number

    while (x !=0) // until x 0
    {
    cin >> x;
    sum += x;
    s ;
    }
    int z = sum / s;
    cout << "sum: " << sum <<
    "ch: " << s-1 <<" to: " << from;

    return 0;
    }

  3. /*Without registration
    */
    #include
    using namespace std;
    int main()
    {
    int x[100];int i = 0; int sum = 0;
    while (true) {

    cin >> x[i];

    sum += x[i];
    int be;
    be = 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,
    average = 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 : {don't read}” +
    $”\n Maximum : {max} ” +
    $”\n Minimum : {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. Draw an isosceles triangle using the symbols ^. The height of a user selects. For example: height = 5, on the screen
    I'm f%#s here 4 hours with while loops, and even their answer contains a for loop. OF COURSE, SO EASIER, FUCK POSTING AN ASSIGNMENT ON THIS TOPIC

  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 *