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

  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 areaofrectangle is
    
    • Defines the main procedure named areaofrectangle.
  3. 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.
  4. Taking User Input:

    Put ("Enter the length: ");
    Get (length);
    Put ("Enter the breadth: ");
    Get (breadth);
    
    • Prompts the user to enter values for length and breadth.
    • Reads the integer values into respective variables.
  5. Calculating the Area:

    area := length * breadth;
    
    • Computes the area using the formula Area = Length × Breadth.
  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 rectangle using the formula Length × Breadth.
  • 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 areaofrectangle.adb
gcc -c areaofrectangle.adb
gnatbind -x areaofrectangle.ali
gnatlink areaofrectangle.ali
$ ./areaofrectangle
Enter the length: 12
Enter the breadth: 6
Area =  72