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;

    int main() {

    int _start = 0;
    int _end = 0;
    int _sumInputNumber = 0;

    cout <> _start;
    cout <> _end;

    for (int i = 0; i != _end+1; i ) {
    if (i % 2 != 0) {
    cout << i << " ";
    _sumInputNumber += i;
    }
    }
    cout << "\nAll the sum of whole odd integers from " << _start << " to " << _end << "are: " << _sumInputNumber;
    return 0;
    }

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

    int main() {

    int _inputNumber;
    string _toxer[100];
    string _hertakanTox;
    string _avelacvoxProblner;

    cout << "Enter the number tu build the tree, number will be a higth of tree" <> _inputNumber;

    for (int i = _inputNumber; i != 0; i–) { // toxeri blood
    for (int x = _inputNumber – i; x != 0; x–) {
    _avelacvoxProblner += ” “;
    }
    for (int j = 0; j != i*2-1; j ) {
    _hertakanTox + = “^”;
    }
    _toxer[i] = _avelacvoxProblner + _hertakanTox;
    _hertakanTox.clear();
    _avelacvoxProblner.clear();
    }
    for (int tpel = 0; nipple != _inputNumber + 1; nipple ++) {
    cout << _toxer[nipple] << endl;
    }
    return 0;
    }

  3. Si

    #include
    #include

    main()
    {
    int visota, i, j;
    printf(“Insert height: “);
    scanf_s(“%d”, &visota);

    for (i = 0; i <= visota; i ) // triangle height
    {
    for (j = 0; j < visota + i; j ) // list the number of characters in a string ( triangle width )
    {
    if (j <= visota – i) //
    {
    printf(" ");
    }
    else
    {
    printf("^");
    }

    }
    printf("\n");

    }

    }

  4. private void PrintTriangle()
    {
    int weigth = int.Parse(txbInput.Text);
    char symbol = char.Parse(txbSymbol.Text);

    for (int i = 0; i < weigth; i )
    {
    for (int j = 1; j <= i; j )
    {
    rtxbOutput.Text += symbol.ToString();
    }
    rtxbOutput.Text += symbol.ToString() + "\n";
    }
    }

  5. #include
    using namespace std;

    int main() {
    char c(‘^’);
    int a = 0;
    while (cin >> a && a > 0) {
    for (int d = 1; d <= a; d++) {
    int totalelok = (1 + (d – 1) * 2);
    string tempstr((1 + (a – 1) * 2), ' ');
    tempstr[tempstr.size() / 2] = c;
    for (int t = 1; t != totalelok; t += 2) {
    tempstr[tempstr.size() / 2 + ((t + 1) / 2)] = c;
    tempstr[tempstr.size() / 2 – ((t + 1) / 2)] = c;
    }
    cout << tempstr << endl;
    }
    }
    system("pause");
    return 0;
    }

  6. int main() {
    setlocale(0, “RU”);
    int h, b, x, Y;
    char sym = ‘^’;
    cout << "Введите высоту равнобедренного треугольника: " <> h;
    b = h + (h – 1);
    x = h;
    y = h;
    for (int i = 1; i <= h; i ) {
    for (int j = 1; j <= b; j ) {
    if (I) {
    cout << " ";
    }
    else {
    cout << sym;
    }
    }
    x -= 1;
    y += 1;
    cout << endl;
    }
    return 0;
    }

    1. I tried to chew 3 task
      The for loop looks like this:

      for ( definition of the control variable ;
      loop repetition condition ;
      changes in the control variable )

      The task is more difficult. Draw an isosceles
      triangle made of ^ symbols. Height selects
      user. For example: height = 5, on the screen

      #include
      using namespace std;

      int main()
      {

      int height = 0; // triangle height

      cout <> height;

      for (int i = 0; i < height; i ) //outer loop for lines
      { // start of outer loop
      // 2 nested loop – for spaces and for characters

      for (int j = 1; j < height – i; j )
      // i is taken from the outer loop
      // and j refers to this inner loop
      {
      cout << ' ';
      }
      //print spaces in a line

      for (int j = height – 2 * i; j <= height; j )
      // i is taken from the outer loop
      // and j already belongs to this inner loop

      //let's say height = 5 , then
      //in the zero line j=5, and i=0, so the sign will be displayed 1 times

      // and in the next line j=3 , i=1, (on the next outer loop)
      // so the sign is output 3 fold ,when j = alternately 3, 4, 5

      {
      cout << '^';
      }
      //sign printing

      cout << endl;
      } // end of outer loop
      return 0;
      }

  7. Third day of learning C++. But I already wrote a similar program in Python

    #include
    #include
    #include
    using namespace std;

    int main()
    {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int i;
    string a = ” “;
    string b = “^”;

    cout <> i;

    you e = 1;

    for (int n = 0; n < i; i–, e += 2) {

    for (int k = 0; k < i; k ) {
    cout << a;
    }

    for (int r = 0; r < e; r++) {
    cout << b;
    }
    cout << "\n";
    }

    return 0;
    }

Leave a Reply

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