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 |

## 128 thoughts on “Tasks: while loops, do while, nested loops”

1. PhilBooyah says:

1. Artem says:

Iterator main loop enters with the value 0, therefore, the first nested loop (if we make the appropriate calculations) j = 1, j = 2, j = 3, j = 4; Since this cycle is responsible for the gaps, we'll get 4 whitespace. Then iterator(i=0) It goes down in the second cycle, wherein j = 1; The second nested loop is responsible for the '^', so the sign will indicate a time.
Iterator enters the main loop with the value 1, therefore, the first nested loop j = 1, j = 2, j = 3; respectively, will be 3 whitespace. We go to the second nested loop: j = 3 j = 4 j = 5; Accordingly, the three will indicate our sign '^'. And so on…

2. it says:

When I enter a fractional number in an endless cycle!!!
I collect on VS 2015

1. it says:

This is the first problem.

2. Aleks says:

try to float

3. Aleks says:

try to float

1. it says:

Well no, It does not help, Yes and should not help. By and large, it only changes the redundant memory.
When I was looking for some fractional number, prog “hangs”…

4. ivaaaan says:

float kr, kp;
int kt = 1, kpn = 0, ktn = 0;
cout <> kr;
Kp = No.;

for (; kp != 0; kt += 2 , kp–)
{
kpn = 0;
while(kp > kpn) {
cout < ktn) {
cout << "^"; ktn++;
}
cout << endl;

5. Nejdan says:

A problem with triangle(3):

#include
#include
#include
using namespace std;

int main()
{
string triangle = “^”;
int height, i = 0;
int eve = 30;
cout <> height;

while (i < height)
{
cout << setw(eve) << triangle << endl;
triangle += "^^";
i ;
eve++;
}

system("pause");
return 0;
}

6. Enderprogramist says:

I read the beginning of the While loop and decided 3 the task of using it :) my solution is different from other, but it works (I'm here podredaktirovalchtoby easier and clearer to read here:

#include
using namespace std;
int main()
{
int i; // height
int probel; // the number of spaces
you TREL = 1; // the number of triangles
int a = 1; /* Since trel postojano reset, kol – in triangles
shall designate this variable */
cout << "введите высоту пирамиды" <> i;
while(i) {
probel = i-1; /*Qty probelov i-1 (height = 5) the number of spaces on the top floor = 4 i.t can see for yourself that it works */
while(probel) {
cout << " "; // Output Blank
probel–;
}

while(Trel) {
cout << "^"; // after writing gaps triangles
Trel–;
}
a += 2; /*triangles pattern – 1, 3, 5, 7.. ie increases
on 2*/
trel = a; // you follow the number of triangles
cout << "\n"; // newline (floor of the pyramid);
i–;
}

return 0;
} /* I tried very hard to hope you enjoyed, obraschyayte no attention to grammar mistakes and RUF. (I'm a newbie)*/

7. newcomer says:

#include
using namespace std;

int main()
{
setlocale(LC_ALL, “ru”);
cout << "программа для вывода треугольника"<<endl;
cout <> height;
for (int j = 0; j < height; j )
{
for (int i = 1; i < height * 2; i )
{
if (i height+j)
cout << " ";
else
cout << "^";

}
cout << endl;
}

}// задачка с треугольником(в vs 2017)

8. GalizMan says:

#include “pch.h”
#include
using namespace std;

int main()
{
int height;
cout <> height;
for (int i = 1; i <= height; i )
{
for (int j = 1; j = height – i + 1) && (j <= height + i – 1)) ? cout << "^" : cout << " ";
}
cout << endl;
}
return 0;
}