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.

  1. Importing I/O Package:

    With Gnat.IO; use Gnat.IO;
    
    • Includes the Gnat.IO package for input (Get) and output (Put_Line) functions.
  2. Declaring the Procedure:

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

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

    area := side * side;
    
    • Computes the area using the formula Area = side × side.
  6. Displaying the Result:

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