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
177 thoughts on “Tasks: while loops, do while, nested loops”
I did it through a for loop
int main() { int c=0; // variable-counter of the number of entered numbers int a; // variable for entering a number int b=0; // variable-counter of the sum of entered numbers for (int i = 0; ;i ) { cout << "Enter number" <> a; c = i; b += a;
if (a == 0) // loop exit condition { break; } } cout << "number of entered numbers "<< c << endl; // display the number of entered numbers cout << "sum of numbers = " << b << endl; //display the sum of the entered numbers return 0; }
3. The task is more difficult. Draw an isosceles triangle using the symbols ^. The height of a user selects. For example: height = 5, on the screen I'm f%#s here 4 hours with while loops, and even their answer contains a for loop. OF COURSE, SO EASIER, FUCK POSTING AN ASSIGNMENT ON THIS TOPIC
int _input_number_i = 0; float _input_number=0; float _input_number_arr[100]; float _input_num_sum=0;
cout << "Enter the ordinal number, if it will be = 0, the programm will stop entering" <> _input_number;
while (_input_number) {
_input_number_arr[_input_number_i] = _input_number; _input_num_sum += _input_number; cin >> _input_number; _input_number_i++; } if (_input_number_i) cout << "There are a " << _input_number_i << " numbers and summa of all numbers is " << _input_num_sum << " ,and the apperanse is " << _input_num_sum / _input_number_i << endl; return 0; }
I did it through a for loop
int main()
{
int c=0; // variable-counter of the number of entered numbers
int a; // variable for entering a number
int b=0; // variable-counter of the sum of entered numbers
for (int i = 0; ;i )
{
cout << "Enter number" <> a;
c = i;
b += a;
if (a == 0) // loop exit condition
{
break;
}
}
cout << "number of entered numbers "<< c << endl; // display the number of entered numbers
cout << "sum of numbers = " << b << endl; //display the sum of the entered numbers
return 0;
}
1 solved the problem “less competent” =)
#include “pch.h”
#include
using namespace std;
int main()
{
int s = 0; // number counter
int sum = 0; // sum
int x = 1; // to enter a number
while (x !=0) // until x 0
{
cin >> x;
sum += x;
s ;
}
int z = sum / s;
cout << "sum: " << sum <<
"ch: " << s-1 <<" to: " << from;
return 0;
}
/*Without registration
*/
#include
using namespace std;
int main()
{
int x[100];int i = 0; int sum = 0;
while (true) {
cin >> x[i];
sum += x[i];
int be;
be = sum;
if (x[i] == 0) {
ser /= i;
cout << i << endl;
for (int j = 0; j < i; j ) {
cout << x[j] << endl;
}
cout << sum<< " "<< to be<<endl;
exit(0);
}
i ;
}
}
//With min and max
using System;
namespace Program
{
class Program
{
static void Main()
{
int counter = 0,
sumAll = 0,
userInput,
average = 0,
min = 0,
max = 0;
while (true)
{
Console.WriteLine(“Please enter a integer : “);
userInput = Convert.ToInt32(Console.ReadLine());
Console.Clear();
if (userInput == 0)
{
Console.WriteLine($”\n\nReult : \n\n ” +
$”Count of integers : {counter}” +
$”\n Summ of integers : {sumAll} ” +
$”\n Averege : {don't read}” +
$”\n Maximum : {max} ” +
$”\n Minimum : {min} “);
break;
}
if(counter == 0)
{
min = userInput;
max = userInput;
}
counter++;
sumAll = userInput + sumAll;
averege = sumAll / counter;
min = (min > userInput) ? userInput : min;
max = (max < userInput) ? userInput : max;
}
}
}
}
void main()
{
setlocale(LC_ALL, “RU”);
int value = 0;
int sum = 0;
int counter = 0;
while (true)
{
cout << "Введите число:" <> value;
if (value != 0)
{
sum += value;
counter += 1;
}
else
{
break ;
}
}
cout << "Cумма: " << sum << endl;
cout << "Счетчик: " << counter << endl;
}
3. The task is more difficult. Draw an isosceles triangle using the symbols ^. The height of a user selects. For example: height = 5, on the screen
I'm f%#s here 4 hours with while loops, and even their answer contains a for loop. OF COURSE, SO EASIER, FUCK POSTING AN ASSIGNMENT ON THIS TOPIC
————– 1 —————-
#include
using namespace std;
int main() {
int _input_number_i = 0;
float _input_number=0;
float _input_number_arr[100];
float _input_num_sum=0;
cout << "Enter the ordinal number, if it will be = 0, the programm will stop entering" <> _input_number;
while (_input_number) {
_input_number_arr[_input_number_i] = _input_number;
_input_num_sum += _input_number;
cin >> _input_number;
_input_number_i++;
}
if (_input_number_i)
cout << "There are a " << _input_number_i << " numbers and summa of all numbers is " << _input_num_sum << " ,and the apperanse is " << _input_num_sum / _input_number_i << endl;
return 0;
}
———— 2 ————-
#include
using namespace std;
int main() {
int _inputNumber = 0;
int _sumInputNumber = 0;
cout << "Enter any whole number" <> _inputNumber;
if (_inputNumber % 2 != 0) {
_sumInputNumber += _inputNumber;
cout << "The numbers sum is " << _sumInputNumber << endl;
}
}
return 0;
}