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} ]

  1. 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).
  2. Declaring the Procedure:

    procedure perimeterofsquare is
    
    • Defines the main procedure named perimeterofsquare.
  3. Variable Declarations:

    side : Integer;
    perimeter : Integer;
    
    • side: Stores the length of one side of the square.
    • perimeter: Stores the calculated perimeter.
  4. 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.
  5. Calculating the Perimeter:

    perimeter := 4 * side;
    
    • Computes the perimeter using the formula Perimeter = 4 × side.
  6. Displaying the Result:

    Put_Line ("Perimeter = " & integer'image(perimeter));
    
    • Converts the integer perimeter to a string using integer'image(perimeter).
    • Prints the calculated perimeter.
  7. 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