SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Area and Perimeter of a circle in PL/SQL

In PL/SQL, which is Oracle's procedural language for SQL, you can define a procedure or a function to calculate the area and perimeter (circumference) of a circle using the formulas:

  • Area = �� * r^2
  • Perimeter (Circumference) = 2 * �� * r

Here's a simple example of how you can create two functions, one for the area and another for the perimeter, of a circle in PL/SQL:

CREATE OR REPLACE PACKAGE CircleCalculations AS
    FUNCTION Area(r NUMBER) RETURN NUMBER;
    FUNCTION Perimeter(r NUMBER) RETURN NUMBER;
END CircleCalculations;

/

CREATE OR REPLACE PACKAGE BODY CircleCalculations AS

    PI CONSTANT NUMBER := 3.141592653589793;

    FUNCTION Area(r NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN PI * r * r;
    END Area;

    FUNCTION Perimeter(r NUMBER) RETURN NUMBER IS
    BEGIN
        RETURN 2 * PI * r;
    END Perimeter;

END CircleCalculations;
/

-- To use the functions:

DECLARE
    rad NUMBER := 5;  -- for a circle with radius 5
    a   NUMBER;
    p   NUMBER;
BEGIN
    a := CircleCalculations.Area(rad);
    p := CircleCalculations.Perimeter(rad);
    DBMS_OUTPUT.PUT_LINE('Area: ' || a);
    DBMS_OUTPUT.PUT_LINE('Perimeter: ' || p);
END;
/

This PL/SQL script creates a package with two functions Area and Perimeter. After creating the package, you can call these functions in other PL/SQL blocks, procedures, or functions to compute the area and perimeter of a circle for a given radius.

Note: Make sure you have privileges to create packages and you might want to adjust the precision of the constant PI or use Oracle's in-built precision for such calculations, if necessary.

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

    DECLARE
        radius NUMBER := 5;
        area NUMBER;
        perimeter NUMBER;
    BEGIN
        area := 3.14 * radius * radius;
        perimeter := 2 * 3.14 * radius;
        DBMS_OUTPUT.PUT_LINE('Area: ' || area);
        DBMS_OUTPUT.PUT_LINE('Perimeter: ' || perimeter);
    END;
    /
    
  2. Oracle SQL function for finding area and perimeter of a circle:

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

    CREATE OR REPLACE PROCEDURE calculate_circle_area_perimeter_proc (radius IN NUMBER) IS
        area NUMBER;
        perimeter NUMBER;
    BEGIN
        area := 3.14 * radius * radius;
        perimeter := 2 * 3.14 * radius;
        DBMS_OUTPUT.PUT_LINE('Area: ' || area);
        DBMS_OUTPUT.PUT_LINE('Perimeter: ' || perimeter);
    END calculate_circle_area_perimeter_proc;
    /