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