
Вычислите сумму: 1+1/2+1/3+1/4+…+1/10. Решите задачу в 2 вариантах - с использованием операторов
while и repeat.

Ответы на вопрос

i:integer;
s:real;
begin
i:=1; s:=1;
while i<>10 do
begin
i:=i+1;
s:=s+1/i;
end;
writeln('Вычисленно с помощью цикла while: ',s);
i:=1; s:=1;
repeat
i:=i+1;
s:=s+1/i;
until i=10;
writeln('Вычисленно с помощью цикла repeat: ',s)
end.



Calculating the Sum of a Series
To calculate the sum of the series 1 + 1/2 + 1/3 + 1/4 + … + 1/10, we can use two different approaches: one using the `while` operator and the other using the `repeat` operator.
Using the `while` Operator
To calculate the sum using the `while` operator, we can iterate through the series and accumulate the sum.
```lua -- Using the while operator local i = 1 local sum = 0 while i <= 10 do sum = sum + 1/i i = i + 1 end print("The sum of the series using while operator is: " .. sum) ``` [[SOURCE #4]]
Using the `repeat` Operator
Alternatively, we can calculate the sum using the `repeat` operator.
```lua -- Using the repeat operator local j = 1 local total = 0 repeat total = total + 1/j j = j + 1 until j > 10 print("The sum of the series using repeat operator is: " .. total) ``` [[SOURCE #9]]
By using either the `while` or `repeat` operator, we can calculate the sum of the given series.


Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili