Вопрос задан 05.08.2018 в 17:47. Предмет Информатика. Спрашивает Костырин Андрей.

Одномерные массивы. Дан массив натуральных чисел, состоящий из n элементов. Найти сумму элементов

массива, кратных данному числу К. (паскаль)
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Перевезенцев Никита.
Const  n = 10;
var  a :array [1..n] of integer;  k, sum :integer;
begin
  readln(k);  for i:= 1 to n do    readln(a[i]);  sum:= 0;  for i:= 1 to n do    if a[i] mod k = 0 then      sum:= sum +a[i];  writeln(sum);
end.
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

One-Dimensional Arrays in Pascal

In Pascal, to find the sum of elements in an array that are multiples of a given number K, you can use a loop to iterate through the array and accumulate the sum of elements that satisfy the given condition.

Here's a sample Pascal code to achieve this:

```pascal program ArraySum;

var arr: array[1..100] of Integer; // Declare an array of natural numbers n, K, i, sum: Integer;

begin // Initialize the array with some values (you can modify this part according to your requirements) n := 10; // Number of elements in the array K := 3; // The number for which multiples we want to find and sum sum := 0; // Initialize the sum

// Populate the array with some values (you can modify this part according to your requirements) for i := 1 to n do begin arr[i] := i; // Assigning some values to the array elements end;

// Calculate the sum of elements that are multiples of K for i := 1 to n do begin if (arr[i] mod K = 0) then // Check if the element is a multiple of K begin sum := sum + arr[i]; // Accumulate the sum end; end;

// Display the sum writeln('The sum of elements in the array that are multiples of ', K, ' is: ', sum); end. ```

In this code, we declare an array of natural numbers, initialize it with some values, and then iterate through the array to find the elements that are multiples of K and accumulate their sum.

This code can be modified according to the specific requirements and the actual values of the array elements.

Note: The provided Pascal code is a sample and may need to be adjusted based on the specific requirements and the actual values of the array elements.

0 0

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

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

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