Calculate Area of a Circle Using PL/SQL
Write a PL/SQL block to calculate the area of a circle for the radius ranging from 3 to 7. Store the radius and corresponding area into the Circle table.
COLUMN | DATATYPE |
---|---|
Radius | NUMBER(5) |
Area | NUMBER(7,2) |
This PL/SQL code calculates the areas of circles for different radii and stores the results in a database table. Let's break it down:
DECLARE: We declare the following variables and a constant to use in our calculations.
- A variable named Radius of type NUMBER(5) to store the radius of the circle.
- A variable named Area of type NUMBER(7,2) to store the calculated area of the circle.
- A constant named Pi of type NUMBER(3,2) and initializes it with the value of 3.14.
BEGIN: Marks the beginning of the executable part of the PL/SQL block.
FOR Loop: Starts a loop that iterates over a range of values for the Radius variable. In this case, the loop will execute for Radius values ranging from 3 to 7 inclusive.
Area Calculation: Inside the loop, the area of the circle is calculated using the formula Pi * Radius * Radius.
INSERT INTO Circle: We store the radius and its corresponding area in a database table called "Circle".
END LOOP: Marks the end of the loop.
END: Marks the end of the PL/SQL block.
In essence, this code automates the process of calculating circle areas for different radii, making it easier to manage and analyze large datasets of circles.
Categories: PL/SQL Hands-on Tags: #Calculate area of a circle, #PL/SQL