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

    using namespace std;

    void main()
    {
    setlocale(LC_ALL, “Rus”);

    int a, crayon-6245184e679b3535057200/;
    int i = 0;
    double sum = 0;

    cout << "Введите числа для расчета (выйти – 0)" << endl;

    do
    {
    i ;
    cout << "Число " << i <> a;
    crayon-6245184e679b3535057200/;
    sum += a;
    } while (a != 0);

    cout << "\n~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "Кол-во введенных чисел: " << crayon-6245184e679b3535057200/ – 1 << endl;
    cout << "Общая сумма: " << sum << endl;
    cout << "Среднее арифметическое: " << (double)(sum / (crayon-6245184e679b3535057200/ – 1)) << endl;

    }

  2. crayon-6245184e679b3535057200/, crayon-6245184e679b3535057200/ 3 crayon-6245184e679b3535057200/)

    1. It is solved more complicated there in the second cycle.

      int main()
      {
      int height = 5;

      // It is solved more complicated there in the second cycle 0 or 1 It is solved more complicated there in the second cycle
      for( int i{0}; i < height ; i )
      {
      // It is solved more complicated there in the second cycle, It is solved more complicated there in the second cycle
      for ( int j{1}; j< It is solved more complicated there in the second cycle; j )
      {
      cout << "*";
      }
      // It is solved more complicated there in the second cycle
      // It is solved more complicated there in the second cycle *2+1 It is solved more complicated there in the second cycle 0
      // and i*h+1 if the lines with 1
      for ( int h{} ; h<and i*h+1 if the lines with ; h++)
      {
      cout << "$";
      }
      cout << endl;
      }
      return 0;
      }

  3. setlocale(LC_ALL, “rus”);
    int heigt = 0;
    int from = 0;
    int before = 0;
    cout <> sacred;
    cout << endl << endl;
    for ( int i = 1; i <= sacred; i )
    {
    from = heigt – i;
    before = from + (i + i – 2);
    for (int j = 0; j <= sacred – i; j )
    {
    cout << " ";
    }
    for (int j = from; j <= before; j )
    {
    cout << "^";
    }
    cout << endl;
    }
    return 0;

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

    char pen = ‘^’;
    int hight = 0;
    int n = 0;
    int d = 1;

    cout <> hight;

    do
    {
    n = 0;
    while (n < hight-1)
    {
    cout << " ";
    n++;
    }

    for (int i = 0; i < d; i )
    {
    cout << rim;
    }
    cout << endl;

    d += 2;
    hight–;

    } while (hight != 0);

    return 0;
    }

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

    int height;
    char symbol;
    cout << "Эта программа рисует равнобедренный треугольник из указанных символов.\n\n"
    <> symbol;
    cout <> height;
    cout << endl;

    int spaceCounter = height;
    int symbolCounter = 1;

    for (int i = 1; i 0; j–) { // Space Cycle
    cout << " ";
    }
    for (int s = 1 ; s <= symbolCounter; s ) { // Character output loop
    cout << symbol;
    }
    symbolCounter += 2;
    cout << endl;
    }
    return 0;
    }

    1. For some reason, when copying the code got corrupted. Corrected:

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

      int height;
      char symbol;
      cout << "Эта программа рисует равнобедренный треугольник из указанных символов.\n\n"
      <> symbol;
      cout <> height;
      cout << endl;

      int spaceCounter = height;
      int symbolCounter = 1;

      for (int i = 1; i 0; j–) { // Space Cycle
      cout << " ";
      }
      for (int s = 1 ; s <= symbolCounter; s ) { // Character output loop
      cout << symbol;
      }
      symbolCounter += 2;
      cout << endl;
      }
      return 0;
      }

  6. #include
    #include
    #include
    #include

    using namespace std;

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

    int numb; //Variable for entering numbers

    int summ=0; // Variable for summing input numbers

    int rep=-1; // Variable counter input, is -1 since the last input is also taken into account

    int maxnumb=0; // variable max number

    int memory=1000000; // Minimum number variable

    int tab[254]; // Array to store numbers

    do
    {
    cout << endl <> numb; // Entering numbers
    rep++; // Counter
    if (numb !=0) tab[rep] = numb; // Entering numbers into an array
    sum = sum + numb; // Sum of input numbers
    if ( memory > numb && numb !=0) minnumb = numb; // Minimum number, a condition was added so that the array termination condition was not displayed
    if ( maxnumb < numb ) maxnumb = numb; // Maximum number
    if (numb !=0) cout << endl << " Press 0 to end." << endl;
    }
    while ( numb != 0 ); // array stop condition
    cout << endl << " Total : " << endl;
    cout << endl << " Maximum number : "<< maxnumb << endl;
    cout << endl << " Minimum number : " << memory <<endl;
    cout << endl << " Number of inputs : " << rep << endl;
    cout << endl << " Sum of numbers : " << summ << endl;
    cout << endl << " Average : " << summ / rep << endl << endl;
    // Array sort loop
    for (int i = 0; i tab[i+1] && tab[i+1] !=0)
    {
    int buff=0;
    buff = tab[i] ;
    tab[i] = tab [i+1];
    tab[i+1] = buff;
    i=-1;
    }

    }
    cout << " Entered numbers : ";

    // Array output loop
    for (int i = 0 ; i<=rep-1; i ) cout <<" " << tab[i] << " ";
    cout << endl<<endl;
    }

  7. int main()
    {

    int size;
    int iterator = 0;
    int symbolIterator = 0;
    int var = 0;
    cin >> size;
    cout << "\n\n\n\n\n";

    char space = ' ';
    char symbol = '^';

    while (iterator <= size) {
    iterator++;
    cout << space;

    if (iterator == size) {
    while (symbolIterator <= was){
    cout << symbol;
    symbolIterator++;}
    symbolIterator = 0;
    was+=2;
    iterator = 0;
    size–;
    cout << endl;
    }
    }
    cout << "\n\n\n\n";

  8. Isosceles triangle problem . for(int j = 1; j < height – i; j ); the compiler does not skip this line of code ,It shows an error – i; Prompt , what's the matter ?

Leave a Reply

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