ADA Program to find the difference of two integer number

Program

With Gnat.IO; use Gnat.IO;
procedure subtwointeger is
	a : Integer;
	b : Integer;
	diff : Integer;
begin
	Put ("Enter value of a: ");
	Get (a);
	Put ("Enter value of b: ");
	Get (b);
	diff := a - b;
	Put_Line ("Difference = " & integer'image(diff));
end;

This Ada program reads two integer numbers from the user, calculates their difference, and displays the result.

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

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

    • Defines the procedure subtwointeger, which is the main program.
  3. Variable Declarations:

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

    • Marks the beginning 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 Difference:

    diff := a - b;
    
    • Subtracts b from a and stores the result in diff.
  7. Displaying the Result:

    Put_Line ("Difference = " & integer'image(diff));
    
    • integer'image(diff) converts the integer diff into a string.
    • Put_Line prints the result 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 difference.
  • Displays the result using integer'image(diff) for correct formatting.

Output

$ gnat make subtwointeger.adb
gcc -c subtwointeger.adb
gnatbind -x subtwointeger.ali
gnatlink subtwointeger.ali
$ ./subtwointeger
Enter value of a: 76
Enter value of b: 13
Difference =  63