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

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

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.



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.


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