PL/SQL Program to Find largest of 3 numbers
Here you will learn pl/sql program to find largest of 3 numbers by using 3 different methods in pl/sql programming.
PL/SQL Program to Find largest of 3 numbers using if else-if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | DECLARE num1 NUMBER := 40; -- First number num2 NUMBER := 50; -- Second number num3 NUMBER := 10; -- Third number largest NUMBER; -- variable to store largest number BEGIN IF (num1 >= num2) AND (num1 >= num3) THEN largest := num1; ELSIF (num2 >= num1) AND (num2 >= num3) THEN largest := num2; ELSE largest := num3; END IF; DBMS_OUTPUT.PUT_LINE('The largest number is: ' || largest); END; / |
Output
The largest number is : 50
PL/SQL Program to Find largest of 3 numbers using Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- Create function to find largest number CREATE OR REPLACE FUNCTION get_largest(num1 IN NUMBER, num2 IN NUMBER, num3 IN NUMBER) RETURN NUMBER IS largest NUMBER; BEGIN -- compare numbers IF (num1 >= num2) AND (num1 >= num3) THEN largest := num1; ELSIF (num2 >= num1) AND (num2 >= num3) THEN largest := num2; ELSE largest := num3; END IF; RETURN largest; -- return largest number END; / |
Calling the Function:
1 2 3 4 5 6 7 | DECLARE result NUMBER; BEGIN result := get_largest(20, 86, 58); -- Call get_largest function and store the result DBMS_OUTPUT.PUT_LINE('The largest number is: ' || result); END; / |
Output
The largest number is: 86
PL/SQL Program to Find largest of 3 numbers using Procedure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | -- Create the procedure to find the largest number CREATE OR REPLACE PROCEDURE find_largest(num1 IN NUMBER, num2 IN NUMBER, num3 IN NUMBER) IS largest NUMBER; -- Variable for storing largest number BEGIN -- Compare numbers IF (num1 >= num2) AND (num1 >= num3) THEN largest := num1; ELSIF (num2 >= num1) AND (num2 >= num3) THEN largest := num2; ELSE largest := num3; END IF; -- Print largest number DBMS_OUTPUT.PUT_LINE('The largest number is: ' || largest); END; / |
Running the Procedure:
1 2 3 4 | BEGIN find_largest(85, 58, 96); -- Call the procedure END; / |
Output
The largest number is: 96
Check out our other PL/SQL programs examples