How many positive integers less than 1000 have the sums of their digits equal to 6?
No idea on how to begin this using Pascal. On Python my script would look something like:
a = 1
b = 1000
for i in range(a,b):
........
I do not know how to access the digits. If someone could give me a heads up, I should be able to make some progress from here.
Here's the solution, without the unnecessary conversion to string. It works by obtaining the right-most digit, adding its value to the accumulator Total
, and then removing the rightmost digit by performing an integer division by 10, and repeating the process until we have nothing left.
var
Value, Digit, Total, NumValues: Integer;
i: Integer;
begin
NumValues := 0;
for i := 1 to 1000 do
begin
Value := i;
Total := 0;
repeat
Digit := Value mod 10;
Total := Total + Digit;
Value := Value div 10;
until Value = 0;
if Total = 6 then
Inc(NumValues);
end;
WriteLn ('I found ', NumValues, ' numbers whose digits add up to six');
ReadLn;
end.