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.

  1. With Gnat.IO; use Gnat.IO;

    • Imports the Gnat.IO package, which provides functions for input and output.
    • use Gnat.IO; allows direct usage of procedures like Put, Put_Line, and Get.
  2. procedure addtwointeger is

    • Defines the procedure addtwointeger, which acts as the main program.
  3. Variable Declarations:

    a : Integer;
    b : Integer;
    sum : Integer;
    
    • Declares three integer variables:
      • a → First number
      • b → Second number
      • sum → Stores the sum of a and b
  4. begin

    • Marks the start of the executable code.
  5. 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); and Get(b); to store values in a and b.
  6. Calculating Sum:

    sum := a + b;
    
    • Adds a and b and stores the result in sum.
  7. Displaying the Result:

    Put_Line ("Sum = " & integer'image(sum));
    
    • integer'image(sum) converts the integer sum into a string.
    • Put_Line prints the sum to the console.
  8. end;

    • Marks the end of the program.

Key Features:

  • Uses Gnat.IO for 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