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”);

    double min_digit = 0;
    double max_digit = 0;
    double sum_digit = 0;

    cout <> min_digit;
    cout <> max_digit;

    for (int i = min_digit; i <= max_digit; i )
    {
    if (i % 2 == 0)
    {
    continue;
    }
    cout << i << " " ;
    sum_digit += i;
    }

    cout << endl;
    cout << "Сумма нечетных чисел в диапазоне от " << min_digit << " to " << max_digit << " = " << sum_digit << endl;

    return 0;
    }

  2. In the first task I wrote differently, this is not a mistake? #include
    using namespace std;

    int main() {
    setlocale(LC_ALL, “”);
    int a = 0;
    int b;
    int g = 0;
    start : cout <> b;
    for (; b != 0;) {
    a++;
    g += b;
    goto start;
    }
    if (a < 5) {
    cout << "Ви ввели " << a << " number" << endl;
    }
    else {
    cout << "Ви ввели " << a << " numbers" << endl;
    }
    cout << "Сумма усіх чисел: " << g << endl;
    cout << "Середнє арифметичне усіх чисел: " << g / a << endl;

    }

  3. Hello ! Most requested ! Give a detailed description of the solution to the problem of constructing a filled triangle from symbols ! If it exists at all . because I can’t find it on the internet ! Thank you in advance , who will respond. Of course , I have my own version , however, I would like to see the specialist’s description.

    1. #include
      using namespace std;
      int main(){
      int a;
      cin>>a;
      int pN=a,sN=1,pn=0,sn=0;
      string p,s;
      p=” “;
      s=”^”;
      while(pN>0){
      while(pn!=pN){
      cout<<p;
      pn+=1;
      }
      pn=0;
      while(sn!=sN){
      cout<<s;
      sn+=1;
      }
      sn=0;
      cout<<endl;
      sN+=2;
      pN-=1;
      }
      return 0;
      }

  4. #include
    using namespace std;

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

    int a = 0;
    cout <> a;

    for (int i = 0; i <= a; i ) {
    for (int j = 0; j = a – i && j <= a + i) cout << "^";
    else cout << " ";
    }
    cout << "\n";
    }
    return 0;
    }

  5. 3-egg task using while
    int main() {
    setlocale(LC_ALL, “RU”);
    int high;
    int checker = 0;
    string three = “^”;
    string tr = “^^”;
    int i = 1;
    int counter;
    cout << "Введите высоту равнобедренного треугольника: " <> high;
    counter = high;
    while (checker < high) {
    for (i; i <= high;i ) {
    cout << " ";
    }
    cout << Three <<endl;
    tre += tr;
    checker++;
    counter–;
    i -= counter ;
    }
    }

  6. 1 The task:

    #include “iostream”
    using namespace std;

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

    int sum = 0, avr, dig, kol = 0, SIZE=0;

    for (int j = 0; ; j ) {
    cin >> dig;
    sum += dig;
    if (dig == 0) break;
    kol++;
    }

    cout << "Количество элементов: " << crayon-6245184e679b3535057200/ << endl;
    cout << "Сумма: " << sum << endl;
    cout << "Ср. арефм.: " << (sum / crayon-6245184e679b3535057200/) << endl;

    return 0;
    }

Leave a Reply

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