Вопрос задан 30.04.2019 в 23:44. Предмет Информатика. Спрашивает Волкова Юлиана.

Написать программу в Pascal, с оператором while и doНайти сумму всех n-значных чисел, если 1 ≤ n ≤ 4

0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Опалько Аня.
Var
  i, sum : integer;
begin
  sum := 0;
  for i := 1 to 9999 do
    sum := sum + i;
  write('Результат: ', sum);
end.
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Writing a Program in Pascal to Find the Sum of All n-Digit Numbers

To write a program in Pascal to find the sum of all n-digit numbers, we can use a combination of the `while` and `do` loops to iterate through the numbers and calculate their sum.

Here's a Pascal program to achieve this:

```pascal program SumOfNDigitNumbers;

var n, i, number, sum: integer;

begin sum := 0;

for n := 1 to 4 do begin i := 1; number := 1;

while i < n do begin number := number * 10; i := i + 1; end;

repeat sum := sum + number; number := number + 1; until number = (number * 10); end;

writeln('The sum of all ', n, '-digit numbers is: ', sum); end. ```

Explanation: - The program starts by initializing the variables `n`, `i`, `number`, and `sum`. - It then uses a `for` loop to iterate through the values of `n` from 1 to 4, representing the number of digits. - Inside the `for` loop, a `while` loop is used to generate the n-digit numbers and calculate their sum. - The `repeat` loop is used to iterate through all the n-digit numbers and add them to the `sum`. - Finally, the program outputs the sum of all n-digit numbers for each value of `n`.

This program calculates the sum of all 1 to 4-digit numbers using the `while` and `do` loops in Pascal.

0 0

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос