You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TO display output in the screen – give the below command initially
set serveroutput on;
1) PL/SQL function to find factorial of a number recursively:
CREATE OR REPLACEFUNCTIONfactorial(n INNUMBER) RETURN NUMBER
IS
BEGIN-- base case
IF n =0OR n =1 THEN
RETURN 1;
ELSE
-- recursive case
RETURN n * factorial(n -1);
END IF;
END;
/
DECLARE
n NUMBER :=5;
fact NUMBER;
BEGIN
fact := factorial(n);
DBMS_OUTPUT.PUT_LINE('Factorial of '|| n ||' is '|| fact);
END;
/
2) PL/SQL function for finding a number is a prime number
CREATE OR REPLACEFUNCTIONis_prime(n INNUMBER) RETURN BOOLEAN
IS
divisor NUMBER :=2;
BEGIN
IF n <=1 THEN
RETURN FALSE;
END IF;
WHILE divisor <= SQRT(n) LOOP
IF MOD(n, divisor) =0 THEN
RETURN FALSE;
END IF;
divisor := divisor +1;
END LOOP;
RETURN TRUE;
END;
/
DECLARE
n NUMBER :=17;
BEGIN
IF is_prime(n) THEN
DBMS_OUTPUT.PUT_LINE(n ||' is a prime number.');
ELSE
DBMS_OUTPUT.PUT_LINE(n ||' is not a prime number.');
END IF;
END;
/
PL/SQL function to retrieve the count of students from ‘CSE’ department from the table Student (Sno, sname, dept, cgpa)
CREATE OR REPLACEFUNCTIONget_cse_student_count RETURN NUMBER
IS
cse_count NUMBER;
BEGINSELECTCOUNT(*) INTO cse_count FROM Student WHERE dept ='CSE';
RETURN cse_count;
END;
/
DECLARE
cse_count NUMBER;
BEGIN
cse_count := get_cse_student_count;
DBMS_OUTPUT.PUT_LINE('Number of students in CSE department: '|| cse_count);
END;
/
4) PL/SQL function to retrieve the maximum CGPA of the student from the table Student (Sno, sname, dept, cgpa)
CREATE OR REPLACEFUNCTIONget_max_cgpa RETURN NUMBER
IS
max_cgpa NUMBER;
BEGINSELECTMAX(cgpa) INTO max_cgpa FROM Student;
RETURN max_cgpa;
END;
/
5) PL/SQL Function that computes and returns the average of two numbers
CREATE OR REPLACEFUNCTIONaverage_of_two_numbers (
num1 NUMBER,
num2 NUMBER
) RETURN NUMBER IS
avg_num NUMBER;
BEGIN
avg_num := (num1 + num2) /2;
RETURN avg_num;
END;
/