ADA Program to find the division of two integer number

Program

With Gnat.IO; use Gnat.IO;
procedure divtwointeger is
	a : Integer;
	b : Integer;
	div : Integer;
begin
	Put ("Enter value of a: ");
	Get (a);
	Put ("Enter value of b: ");
	Get (b);
	div := a / b;
	Put_Line ("Divison = " & integer'image(div));
end;

This Ada program takes two integer inputs from the user, performs division, and displays the result.

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

    • Imports the Gnat.IO package to use input (Get) and output (Put_Line) functions.
  2. procedure divtwointeger is

    • Defines the main procedure named divtwointeger.
  3. Variable Declarations:

    a : Integer;
    b : Integer;
    div : Integer;
    
    • Declares three integer variables:
      • a → First number (dividend)
      • b → Second number (divisor)
      • div → Stores the result of integer division (a / b)
  4. 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.
  5. Performing Division:

    div := a / b;
    
    • Performs integer division (quotient only, decimal part is truncated).
  6. Displaying the Result:

    Put_Line ("Division = " & integer'image(div));
    
    • Converts the integer div into a string using integer'image(div).
    • Prints the result to the console.
  7. end;

    • Marks the end of the procedure.

Key Takeaways:

  • Uses integer division (a / b), so only the quotient is displayed.
  • Needs an additional check to prevent division by zero.
  • Could be improved by using floating-point numbers for accurate division results.

Output 1

$ gnat make divtwointeger.adb
gcc -c divtwointeger.adb
gnatbind -x divtwointeger.ali
gnatlink divtwointeger.ali
$ ./divtwointeger
Enter value of a: 8
Enter value of b: 4
Divison =  2

Output 2

$ gnat make divtwointeger.adb
gcc -c divtwointeger.adb
gnatbind -x divtwointeger.ali
gnatlink divtwointeger.ali
$ ./divtwointeger
Enter value of a: 12
Enter value of b: 0
raised CONSTRAINT_ERROR : divtwointeger.adb:11 divide by zero