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.
-
With Gnat.IO; use Gnat.IO;- Imports the
Gnat.IOpackage to use input (Get) and output (Put_Line) functions.
- Imports the
-
procedure divtwointeger is- Defines the main procedure named
divtwointeger.
- Defines the main procedure named
-
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)
- Declares three integer variables:
-
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.
-
Performing Division:
div := a / b;- Performs integer division (quotient only, decimal part is truncated).
-
Displaying the Result:
Put_Line ("Division = " & integer'image(div));- Converts the integer
divinto a string usinginteger'image(div). - Prints the result to the console.
- Converts the integer
-
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