Forum

Calculating the Sum...
 
Share:
Notifications
Clear all

Calculating the Sum and Average of Three Numbers in PL/SQL


Posts: 1
Guest
Topic starter
(@Anonymous)
New Member
Joined: 8 months ago

Calculating the Sum and Average of Three Numbers in PL/SQL

 

---------------------------------------------------
|   Num1   |   Num2   |   Num3   |   Sum   | Average |
---------------------------------------------------
|    10    |    15    |    20    |   45    |   15    |
---------------------------------------------------
|    5     |    12    |    8     |   25    |  8.33   |
---------------------------------------------------
|    18    |    25    |    30    |   73    |  24.33  |
---------------------------------------------------

Introduction:
In the world of programming, PL/SQL (Procedural Language/Structured Query Language) plays a vital role in database management and data manipulation. If you're looking to perform calculations on three numbers, specifically finding their sum and average using PL/SQL, this article is for you. We'll guide you through the process, providing a clear explanation and example code snippets to help you understand and implement this task efficiently.

Query:

To calculate the sum and average of three numbers in PL/SQL, you can use the following query:

```sql
DECLARE
num1 NUMBER := 10; -- Replace with your desired value
num2 NUMBER := 20; -- Replace with your desired value
num3 NUMBER := 30; -- Replace with your desired value
sum_result NUMBER;
average_result NUMBER;
BEGIN
sum_result := num1 + num2 + num3;
average_result := sum_result / 3;

DBMS_OUTPUT.PUT_LINE('Sum: ' || sum_result);
DBMS_OUTPUT.PUT_LINE('Average: ' || average_result);
END;
/
```

Explanation:

1. The `DECLARE` keyword is used to define variables that will be used within the PL/SQL block.
2. We declare three variables: `num1`, `num2`, and `num3`, which represent the three numbers you want to calculate the sum and average of. Feel free to replace these values with your desired numbers.
3. Next, we declare `sum_result` and `average_result` variables to store the results of the calculations.
4. The actual calculations are performed within the `BEGIN` and `END;` block. We add `num1`, `num2`, and `num3` together to obtain the sum and assign it to `sum_result`. Then, we divide `sum_result` by 3 to calculate the average and assign it to `average_result`.
5. Finally, we use the `DBMS_OUTPUT.PUT_LINE` function to display the sum and average results in the output console.

Conclusion:
In this article, we explored how to calculate the sum and average of three numbers using PL/SQL. We provided you with a query that can be executed in a PL/SQL environment, demonstrating a step-by-step approach to achieve the desired results. By understanding and implementing this query, you'll have the necessary skills to perform similar calculations in your PL/SQL projects.

Leave a reply

Author Name

Author Email

Title *

 
Preview 0 Revisions Saved
Share: