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. #include

    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “RUS”);
    int countNum = 0;
    int sum = 0;
    int avarage = 0;
    int number = 1;
    while(number != 0)
    {
    cout <> number;
    sum += number;
    countNum++;
    avarage += number / 2;
    if(number != 0)
    cout << "Чтобы остановить ввод, enter 0." << endl;
    }
    cout << "Количество чисел: " << countNum << endl;
    cout << "Общая сумма чисел: " << sum << endl;
    cout << "Средне арифметическое: " << avarage << endl;
    return 0;
    }

  2. Help to solve 2 tasks
    1) enter a number. Get another number by removing the digits 0 and 1 maintaining the order of the digits. Example(201304, результат 234)
    2) serial input numbers to find their product, entry stop if the work was still 0.

    1. to understand how the first problem is solved through the input array, and a second cycle for any while, or.

      example solutions 2 tasks
      #include
      using namespace std;

      int main()
      {
      setlocale(LC_ALL, “rus”);
      int i,sum;
      while (sum != 0)
      {
      cout << "Введите число" <> i;
      sum = sum + i;
      cout << sum << endl;
      }
      cout << "Сумма чисел = " << sum << endl;
      return 0;
      }

      1. I do not know how to write here the normal komenty (instead of copy-paste script garbage copied)

  3. #include
    using namespace std;
    int main()
    {

    char ch;
    int height;
    cout<>height;
    cout<>ch;

    for (int i = 0; i= 0; j–)
    {
    cout << " ";
    }
    for (int j = 0; j < i+1+i; j )
    {
    cout << ch;
    }

    cout << endl;

    }

    }

  4. How to write C ++ code to do while loop? *draw a triangle and a rectangle, so inside they were empty

    1. only action “draw” It refers to graphics, but has almost nothing to do with C ++ language. It all depends on whether, that you use: a). operating system (Windows, Linux, et al.), b). from graphics system (libraries) in which you want to write it all (Qt, GTK+, wxWidget, MFC, or even in the terminal pseudographics).
      Graphics does not have a part of the C ++ and libraries.
      The very formulation of the problem so – It is not correct!

  5. everything is working, but really turned zamorochitsya. certainly have a lot of excess. to your fine, practical and quick solutions to me far. while newcomer. but as in any way did! Hooray!

    #include
    using namespace std;

    void main()
    {
    setlocale(LC_ALL, “ru”);
    int height;
    int a = 0;
    int c = 0;
    int f = 0;
    int x = 0;
    int z = 0;
    cout <> height;
    int d = height;
    do
    {
    if (a < height – 1)
    {
    do
    {
    cout << " ";
    c++;
    } while (c < height – 1);
    }
    x++;
    c = x;
    do
    {
    cout < 0)
    {
    do
    {
    cout < 0);
    }
    } while (d != height);
    cout << endl;
    with ++;
    f = z;
    a++;
    } while (a < height);
    }

  6. #include
    #include
    using namespace std;
    void main()
    {
    setlocale(LC_ALL, “rus”);
    int n = 3;
    double *mass = new double[n];
    int i = 1,sum=0,ch=0,sred;
    mass[0] = 1;
    while (mass[i-1] != 0)
    {
    cout << "Введите " << i <> mass[i];
    sum = sum + mass[i];
    if(mass[i]!=0)
    ch = ch + 1;
    n++;
    i ;
    }
    sred = sum / ch;
    cout << "Среднее арифметическое из " << ch << " number = " << sred << " . Their total amount = " << sum << endl;
    delete mass;
    system("pause");
    }

  7. Also not been able to see a pattern in the triangle, and because of this extra cycle and extra variables :|

    #include
    #include
    using namespace std;

    int main(int argc, char** argv)
    {
    int x;
    int n = 1;
    int num = 1;
    cout <> x;
    for (int i = 1; i < x; ++i)
    a = a + 2;
    while (x != 0)
    {
    for(int j=1; j < on one; ++j)
    cout << " ";
    for(int i=0; i < n; ++i)
    cout << "^";
    cout << "\n";
    n = n + 2;
    a = a – 1;
    –x;
    }
    system ("pause");
    return 0;
    }

  8. As the lesson was called “while and do while loops” did the 3rd task with their help. I had to tinker because the for loop is more compact and more convenient for this
    #include
    using namespace std;

    int main()
    {
    setlocale(LC_ALL, “rus”);

    int width = 1;
    int j = 1;
    int height = 0;
    int i = 1;
    int space = 1;

    cout << "Введите высоту равноберенного треугольника" <> height;

    do {
    while (probel <= height – i)
    {
    cout << " ";
    probel++;
    }
    while (j <= width)
    {
    cout << "^";
    j ;

    }
    cout << endl;
    i ;
    width += 2;
    j = 1;
    space = 1;
    } while (i <= height);

    system("pause");

    return 0;
    }

Leave a Reply

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