Sum and average of three numbers in PL/SQL

Prerequisite PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.

Given three numbers and the task is to find out the sum and average of three numbers.
Examples:

Input: a = 12, b = 15, c = 32
Output: sum = 59 avg = 19.66

Input: a = 34, b = 45, c = 11
Output: sum = 90 avg = 30

Approach is to take three numbers and find their sum and average using the formula given below-
Sum: a+b+c
Average: (a+b+c)/3
Where a,b,c are the three numbers.

Below is the required implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

--To find sum and avg of three numbers 
DECLARE 
  
    -- Assigning 12 into a 
    a     NUMBER := 12; 
      
    -- Assigning 14 into b 
    b     NUMBER := 14; 
      
    -- Assigning 20 into c 
    c     NUMBER := 20; 
      
    -- Declare variable for sum  
    -- Of Three number (a, b, c) 
    sumOf3 NUMBER; 
      
    -- Declare variable for average 
    avgOf3 NUMBER; 
      
--Start Block 
BEGIN 
  
    -- Assigning sum of a, b and c into sumOf3 
    sumOf3 := a + b + c; 
  
    -- Assigning average  of a, b and c into avgOf3 
    avgOf3 := sumOf3 / 3; 
  
    --print Result  sum of a, b, c number 
    dbms_output.Put_line('Sum = ' 
                         ||sumOf3); 
  
    --print Average  sum of a, b, c number 
    dbms_output.Put_line('Average = ' 
                         ||avgOf3); 
END
  
--End Program 

chevron_right


Output:

Sum = 46
Average = 15.33


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.