Your question, your audience. Choose who sees your identityâ€”and your questionâ€”with question security.

Write a program to compute the sum of two positive integer numbers. Each number is up to 20 digits. Do not use any REAL or LONGINT types in the program.

Must use stacks, digits must be read in one-by-one. For example 2345678 + 9999999999, must be read in 2, then 3, then 4, and so on. if sum of two digits is greater than 9, u must carry it over, so the next part can use it.

Must use stacks, digits must be read in one-by-one. For example 2345678 + 9999999999, must be read in 2, then 3, then 4, and so on. if sum of two digits is greater than 9, u must carry it over, so the next part can use it.

1 Solution

s1,s2,s3 : String;

j,j1,j2,j3,max,carry1 : Byte;

begin

Write( 'enter first number: ' );

Readln( s1 );

Write( 'enter second number: ' );

Readln( s2 );

max := Length( s1 );

if max < Length( s2 ) then max := Length( s2 );

while Length( s1 ) < max do s1 := '0' + s1;

while Length( s2 ) < max do s2 := '0' + s2;

carry1 := 0; s3 := 0;

for j := max downto 1 do begin

j1 := Ord( s1[j] ) - $30; {ASCII -> value }

j2 := Ord( s2[j] ) - $30;

j3 := j1 + j2 + carry1;

carry1 := j3 div 10;

j3 := j3 mod 10 + $30;

s3 := Chr( j3) + s3;

end; {for}

if carry1 = 1 then s3 := '1' + s3;

writeln( 'result: ', s3 );

end.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Tackle projects and never again get stuck behind a technical roadblock.

Join Now