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.
View code
задача do while c++
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intamountDigits=0;// количество введенных чисел
doubletotalSum=0;// общая сумма
doubledigit=0;//
cout<<"Введите числа, для расчета (выйти - 0)\n\n";
do
{
cout<<"Число "<<amountDigits+1<<" = ";
cin>>digit;
if(digit)// если digit любое значение кроме 0(false)
{
amountDigits++;
totalSum+=digit;
}
}while(digit);// пока digit любое значение отличное от 0
2. It is necessary to sum up all the odd integers, which will introduce the user to the keyboard.
View code
задача while c++
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intstart=0;// начало д-на
intfinish=0;// конец д-на
intsumUneven=0;
cout<<"Введите начало диапазона: ";
cin>>start;
cout<<"Введите конец диапазона: ";
cin>>finish;
inti=start;// управляющая переменная
while(i<=finish)
{
if(i%2!=0)
{
cout<<i<<" ";// показать нечетные через пробел
sumUneven+=i;// накапливать их сумму
}
i++;
}
cout<<"\nСумма нечетных чисел в диапазоне от "<<start<<" по "<<finish;
cout<<" = "<<sumUneven<<endl<<endl;
return0;
}
Result:
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
View code
задача - вложенные циклы C++
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
usingnamespacestd;
intmain()
{
setlocale(LC_ALL,"rus");
intheight=0;// высота треугольника
cout<<"Введите высоту равнобедренного треугольника: ";
cin>>height;
for(inti=0;i<height;i++)
{
for(intj=1;j<height-i;j++)
{
cout<<' ';
}
for(intj=height-2*i;j<=height;j++)
{
cout<<'^';
}
cout<<endl;
}
return0;
}
Result:
4.8
64
176 thoughts on “Tasks: while loops, do while, nested loops”
#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; }
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("^"); }
#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;
}
—————- 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;
}
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");
}
}
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";
}
}
#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;
}
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;
}
I can’t figure out the triangle even with the code) what is in the triangle i and j?
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;
}
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;
}