ADA Program to find the sum of two integer number
Program
With Gnat.IO; use Gnat.IO;
procedure addtwointeger is
a : Integer;
b : Integer;
sum : Integer;
begin
Put ("Enter value of a: ");
Get (a);
Put ("Enter value of b: ");
Get (b);
sum := a + b;
Put_Line ("Sum = " & integer'image(sum));
end;
This Ada program reads two integer numbers from the user, calculates their sum, and displays the result.
-
With Gnat.IO; use Gnat.IO;- Imports the
Gnat.IOpackage, which provides functions for input and output. use Gnat.IO;allows direct usage of procedures likePut,Put_Line, andGet.
- Imports the
-
procedure addtwointeger is- Defines the procedure
addtwointeger, which acts as the main program.
- Defines the procedure
-
Variable Declarations:
a : Integer; b : Integer; sum : Integer;- Declares three integer variables:
a→ First numberb→ Second numbersum→ Stores the sum ofaandb
- Declares three integer variables:
-
begin- Marks the start of the executable code.
-
Taking User Input:
Put ("Enter value of a: "); Get (a); Put ("Enter value of b: "); Get (b);- Prompts the user to enter two integer values.
- Uses
Get(a);andGet(b);to store values inaandb.
-
Calculating Sum:
sum := a + b;- Adds
aandband stores the result insum.
- Adds
-
Displaying the Result:
Put_Line ("Sum = " & integer'image(sum));integer'image(sum)converts the integersuminto a string.Put_Lineprints the sum to the console.
-
end;- Marks the end of the program.
Key Features:
- Uses
Gnat.IOfor input and output operations. - Takes two integer inputs and computes their sum.
- Displays the result using
integer'image(sum)for correct formatting.
Output
$ gnat make addtwointeger.adb
gcc -c addtwointeger.adb
gnatbind -x addtwointeger.ali
gnatlink addtwointeger.ali
$ ./addtwointeger
Enter value of a: 65
Enter value of b: 14
Sum = 79