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”
Calculate and display in the form of a table the values of the function F in the interval from Xinit. to Hkon. with step dX. F = ax ^ – bx •- with at X 3 and b = 0 in other cases where a, B, c - real numbers. Function F must be valid, if the expression Structured Programming is NOT(Ac OR Vc) And (Ac MOD2 Sc) not equal to zero, and integer otherwise. Through Ac, Vc and Cc denote the integer parts of the values a, B, with, operations NOT, And, OR and MOD2 (modulo addition 2) - bitwise. The values of a, B, with, Hnach., Hkon., dX enter from keyboard
Given positive integers N and K. Using only addition and subtraction operations, find the quotient of dividing N by K, and also the remainder of this division
#include using namespace std; int main() { setlocale(LC_ALL, “Rus”); int sum = 0, lot = 0, num = 1; cout << "Введите любое кол-во чисел чтобы узнать их общую сумму, среднее арифметическое и кол-во введенных чисел.\nВведите '0' to stop." << endl; for (;on one != 0;) { cout <> on one; on one != 0 ? lot++ : lot = lot; on one != 0 ? sum = sum + on one : sum = sum; } cout << "Общая сумма: " << sum << endl << "Среднее арифметическое: " << sum / lot << endl << "Кол-во введенных чисел: " << lot << endl; return 0; }
Calculate and display in the form of a table the values of the function F in the interval from Xinit. to Hkon. with step dX.
F = ax ^ – bx •- with
at X 3 and b = 0
in other cases
where a, B, c - real numbers.
Function F must be valid, if the expression Structured Programming is NOT(Ac OR Vc) And (Ac MOD2 Sc) not equal to zero, and integer otherwise. Through Ac, Vc and Cc denote the integer parts of the values a, B, with, operations NOT, And, OR and MOD2 (modulo addition 2) - bitwise. The values of a, B, with, Hnach., Hkon., dX enter from keyboard
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Russian”);
int sum,piston,a;
piston = 0;
sum = 0;
cout << "Нажмите любую клавишу\n " << "…………….\n";
while (_getch() != 48) {
cout < “;
cin >> a;
piston ++;
sum = sum + a;
cout << "Чтобы продолжить нажмите любую клавишу \n " << "Чтобы завершить нажмите '0'\n";
}
cout << "Количество чисел = " << piston << endl << "Сумма = " << sum << endl;
system("PAUSE");
return 0;
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “RU”);
int countNumber = 0;
int sumNumber = 0;
while (true)
{
int userNumber;
cout << "Введите число : " <> userNumber;
if (userNumber == 0) {
cout << "Общее количество чисел : " << countNumber << endl
<< "Сумма всех чисел : " << sumNumber << endl
<< "Среднее арифметическое : " << sumNumber / countNumber << endl;
break;
}
countNumber++;
sumNumber = sumNumber + userNumber;
}
}
#include
using namespace std;
void main()
{
setlocale(LC_ALL, “ru”);
cout <> h;
int space = h – 1;
int block = 1;
for (int i = 1; i <= h; i )
{
for (int j = 1; j <= space; j )
cout << " ";
for (int j = 1; j <=block; j )
cout << "^";
space–;
block += 2;
cout << endl;
}
}
Given positive integers N and K. Using only addition and subtraction operations, find the quotient of dividing N by K, and also the remainder of this division
Given an integer N (>1). If it is simple, that is, it has no positive divisors, Besides 1 and myself, then output True, otherwise output False
int main()
{
int h = 0;
char triangeBody[100] = “^”;
cout <> h;
int counterSpaces = 1;
for (int i = 0; i = 1; j–) {
cout << " ";
}
cout << triangeBody << endl;
strcat_s(triangeBody, "^^");
counterSpaces++;
}
}
#include
using namespace std;
int main()
{
setlocale(LC_ALL, “Rus”);
int sum = 0, lot = 0, num = 1;
cout << "Введите любое кол-во чисел чтобы узнать их общую сумму, среднее арифметическое и кол-во введенных чисел.\nВведите '0' to stop." << endl;
for (;on one != 0;)
{
cout <> on one;
on one != 0 ? lot++ : lot = lot;
on one != 0 ? sum = sum + on one : sum = sum;
}
cout << "Общая сумма: " << sum << endl << "Среднее арифметическое: " << sum / lot << endl << "Кол-во введенных чисел: " << lot << endl;
return 0;
}