Написать рекурсивную функцию подсчета суммы цифр натурального числа и с ее использованием определить в каком из двух заданных натуральных чисел сумма цифр больше. ( в Pascal ABC)
program SumOfDigits; function CountDigitsSum(number: integer): integer; begin if number < 10 then CountDigitsSum := number else CountDigitsSum := number mod 10 + CountDigitsSum(number div 10); end; var num1, num2, sum1, sum2: integer; begin writeln('Enter the first number: '); readln(num1); writeln('Enter the second number: '); readln(num2); sum1 := CountDigitsSum(num1); sum2 := CountDigitsSum(num2); writeln('Sum of digits of the first number: ', sum1); writeln('Sum of digits of the second number: ', sum2); if sum1 > sum2 then writeln('Sum of digits of the first number is greater.') else if sum2 > sum1 then writeln('Sum of digits of the second number is greater.') else writeln('Sum of digits of both numbers are equal.'); end.
Пример использования:
Enter the first number: 123 Enter the second number: 456 Sum of digits of the first number: 6 Sum of digits of the second number: 15 Sum of digits of the second number is greater.
function CountDigitsSum(number: integer): integer;
begin
if number < 10 then
CountDigitsSum := number
else
CountDigitsSum := number mod 10 + CountDigitsSum(number div 10);
end;
var
num1, num2, sum1, sum2: integer;
begin
writeln('Enter the first number: ');
readln(num1);
writeln('Enter the second number: ');
readln(num2);
sum1 := CountDigitsSum(num1);
sum2 := CountDigitsSum(num2);
writeln('Sum of digits of the first number: ', sum1);
writeln('Sum of digits of the second number: ', sum2);
if sum1 > sum2 then
writeln('Sum of digits of the first number is greater.')
else if sum2 > sum1 then
writeln('Sum of digits of the second number is greater.')
else
writeln('Sum of digits of both numbers are equal.');
end.
Пример использования:
Enter the first number:123
Enter the second number:
456
Sum of digits of the first number: 6
Sum of digits of the second number: 15
Sum of digits of the second number is greater.