-
Notifications
You must be signed in to change notification settings - Fork 1
/
DigSum.pas
executable file
·67 lines (58 loc) · 2.19 KB
/
DigSum.pas
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
program DigSum;
// Структура данных представляющая пользовательский ввод.
type InputData = record
N: Integer; // натуральное число.
// Диапазон для значений К:
a: Integer;
b: Integer;
end;
// Функция для подсчета цифр числа.
function SumOfDigits(n: Integer): Integer;
begin
Result := 0;
while n <> 0 do begin
Result := Result + n mod 10;
n := n div 10;
end;
end;
// Процедура поиска делителей суммы цифр натурального числа.
procedure Search(myData: InputData);
var
i, s: Integer;
begin
s := SumOfDigits(myData.N);
writeln('Сумма цифр числа ', myData.N, ' составляет ', s, '.');
for i := myData.a to myData.b do begin
if s mod i = 0 then
writeln('K = ', i, '; ', s, ' / ', i, ' = ', s div i, '.');
end;
end;
// Функция для получения данных от пользователя.
function Input(): InputData;
begin
Result.N := 0;
while Result.N <= 0 do begin
write('Введите натуральное число: ');
readln(Result.N);
end;
Result.a := 0; Result.b := 0;
while (Result.a <= 0) or (Result.b <= 0) or (Result.a > Result.b) do begin
writeln('Введите натуральный диапазон (a < b) для поиска делителей суммы цифр натурального числа: ');
readln(Result.a, Result.b);
end;
end;
var
myData: InputData;
choice: Char;
begin
writeln('Назначением данной программы состоит в поиске делителей суммы цифр натурального числа.');
while TRUE do begin
myData := Input();
Search(myData); // запуск поиска делителей.
// Программа завершается запросом на повтор расчетов:
write('Для повтора введие Да или да. Повторить? ');
readln(choice);
if (choice <> 'Д') and (choice <> 'д') then
break;
end;
end.