ADA Program to find area of rectangle
Program
With Gnat.IO; use Gnat.IO;
procedure areaofrectangle is
length : Integer;
breadth : Integer;
area : Integer;
begin
Put ("Enter the length: ");
Get (length);
Put ("Enter the breadth: ");
Get (breadth);
area := length * breadth;
Put_Line ("Area = " & integer'image(area));
end;
This Ada program calculates the area of a rectangle by taking the length and breadth as input from the user and computing the result using the formula:
[ \text{Area} = \text{Length} \times \text{Breadth} ]
-
Importing I/O Package:
With Gnat.IO; use Gnat.IO;- This includes the
Gnat.IOpackage, which provides functions for input (Get) and output (Put_Line).
- This includes the
-
Declaring the Procedure:
procedure areaofrectangle is- Defines the main procedure named
areaofrectangle.
- Defines the main procedure named
-
Variable Declarations:
length : Integer; breadth : Integer; area : Integer;length: Stores the length of the rectangle.breadth: Stores the breadth of the rectangle.area: Stores the calculated area.
-
Taking User Input:
Put ("Enter the length: "); Get (length); Put ("Enter the breadth: "); Get (breadth);- Prompts the user to enter values for
lengthandbreadth. - Reads the integer values into respective variables.
- Prompts the user to enter values for
-
Calculating the Area:
area := length * breadth;- Computes the area using the formula Area = Length × Breadth.
-
Displaying the Result:
Put_Line ("Area = " & integer'image(area));- Converts the integer
areato a string usinginteger'image(area). - Prints the calculated area.
- Converts the integer
-
End of the Procedure:
end;- Marks the end of the procedure.
Key Takeaways:
- This program calculates the area of a rectangle using the formula Length × Breadth.
- Uses
Gnat.IOfor user input and output. - Can be improved by handling negative values and using floating-point numbers for better precision.
Output
$ gnat make areaofrectangle.adb
gcc -c areaofrectangle.adb
gnatbind -x areaofrectangle.ali
gnatlink areaofrectangle.ali
$ ./areaofrectangle
Enter the length: 12
Enter the breadth: 6
Area = 72