ADA Program to find area of square
Program
With Gnat.IO; use Gnat.IO;
procedure areaofsquare is
side : Integer;
area : Integer;
begin
Put ("Enter the dimension of a side: ");
Get (side);
area := side * side;
Put_Line ("Area = " & integer'image(area));
end;
This Ada program calculates the area of a square by taking the length of one side as input from the user and computing the result using the formula Area = side × side.
-
Importing I/O Package:
With Gnat.IO; use Gnat.IO;- Includes the
Gnat.IOpackage for input (Get) and output (Put_Line) functions.
- Includes the
-
Declaring the Procedure:
procedure areaofsquare is- Defines the main procedure named
areaofsquare.
- Defines the main procedure named
-
Variable Declarations:
side : Integer; area : Integer;side: Stores the length of one side of the square.area: Stores the calculated area of the square.
-
Taking User Input:
Put ("Enter the dimension of a side: "); Get (side);- Prompts the user to enter the side length.
- Reads the integer value into the variable
side.
-
Calculating the Area:
area := side * side;- Computes the area using the formula Area = side × side.
-
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 square using the formula side².
- 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 areaofsquare.adb
gcc -c areaofsquare.adb
gnatbind -x areaofsquare.ali
gnatlink areaofsquare.ali
$ ./areaofsquare
Enter the length of one side: 6
Area = 36