The Ada Program: palin.adb
1
2
3 with Text_IO, Ada.Integer_Text_IO;
4 use Ada;
5
6 procedure Palin is
7
8 function Make_Palindrom (N, Left: Natural) return Natural is
9 Result : Natural := Left;
10 H : Natural;
11 begin
12 if (N mod 2 = 0) then
13 H := Left;
14 else
15 H := Left/10;
16 end if;
17 for I in 1..N/2 loop
18 Result := Result * 10;
19 Result := Result + H mod 10;
20 H := H / 10;
21 end loop;
22 return Result;
23 end Make_Palindrom;
24
25 Value, Target, Left_Half : Natural;
26
27 Buffer: String (1..100);
28 Length: Natural;
29
30 begin
31
32 Input : loop
33 Text_IO.Get_Line (Buffer, Length);
34 exit Input when Length <= 1;
35 Value := Integer'Value (Buffer(1..Length));
36 Left_Half := Integer'Value (Buffer(1..(Length-1)/2 + 1));
37 Target := Make_Palindrom (Length, Left_Half);
38 if Target < Value then
39 Target := Make_Palindrom (Length, Left_Half+1);
40 end if;
41 Integer_Text_IO.Put (Target-Value, Width => 0);
42 Text_IO.New_Line;
43
44 end loop Input;
45
46
47 end Palin;
48
49
50
51
52
53