SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Area and Perimeter of Rectangle in PL/SQL

To compute the area and perimeter of a rectangle using PL/SQL, the formulas are straightforward:

  • Area = length �� width
  • Perimeter = 2 �� (length + width)

Here's an example of how you can create two functions in a PL/SQL package to compute the area and perimeter of a rectangle:

CREATE OR REPLACE PACKAGE RectangleCalculations AS
    FUNCTION Area(length NUMBER, width NUMBER) RETURN NUMBER;
    FUNCTION Perimeter(length NUMBER, width NUMBER) RETURN NUMBER;
END RectangleCalculations;
/

CREATE OR REPLACE PACKAGE BODY RectangleCalculations AS

    FUNCTION Area(length NUMBER, width NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN length * width;
    END Area;

    FUNCTION Perimeter(length NUMBER, width NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN 2 * (length + width);
    END Perimeter;

END RectangleCalculations;
/

-- To use the functions:

DECLARE
    len NUMBER := 10;  -- example length
    wid NUMBER := 5;   -- example width
    a   NUMBER;
    p   NUMBER;
BEGIN
    a := RectangleCalculations.Area(len, wid);
    p := RectangleCalculations.Perimeter(len, wid);
    DBMS_OUTPUT.PUT_LINE('Area: ' || a);
    DBMS_OUTPUT.PUT_LINE('Perimeter: ' || p);
END;
/

After defining this package, you can call the Area and Perimeter functions wherever you need in your PL/SQL code to compute the area and perimeter of a rectangle given its length and width.

Note: Ensure you have the necessary privileges to create packages in your Oracle database.

  1. PL/SQL program to calculate area and perimeter of a rectangle:

    DECLARE
        length NUMBER := 5;
        width NUMBER := 3;
        area NUMBER;
        perimeter NUMBER;
    BEGIN
        area := length * width;
        perimeter := 2 * (length + width);
        DBMS_OUTPUT.PUT_LINE('Area: ' || area);
        DBMS_OUTPUT.PUT_LINE('Perimeter: ' || perimeter);
    END;
    /
    
  2. Oracle SQL function for finding area and perimeter of rectangle:

    CREATE OR REPLACE FUNCTION calculate_rectangle_area_perimeter (
        length IN NUMBER, width IN NUMBER)
    RETURN VARCHAR2 IS
        area NUMBER;
        perimeter NUMBER;
        result VARCHAR2(100);
    BEGIN
        area := length * width;
        perimeter := 2 * (length + width);
        result := 'Area: ' || TO_CHAR(area) || ', Perimeter: ' || TO_CHAR(perimeter);
        RETURN result;
    END calculate_rectangle_area_perimeter;
    /
    
  3. PL/SQL procedure for rectangle area and perimeter computation:

    CREATE OR REPLACE PROCEDURE calculate_rectangle_area_perimeter_proc (
        length IN NUMBER, width IN NUMBER) IS
        area NUMBER;
        perimeter NUMBER;
    BEGIN
        area := length * width;
        perimeter := 2 * (length + width);
        DBMS_OUTPUT.PUT_LINE('Area: ' || area);
        DBMS_OUTPUT.PUT_LINE('Perimeter: ' || perimeter);
    END calculate_rectangle_area_perimeter_proc;
    /