0% found this document useful (0 votes)
30 views3 pages

PL/SQL Code Examples with Outputs

The document contains several PL/SQL programs demonstrating various functionalities, including displaying patterns, generating Fibonacci sequences, retrieving top employees by salary, calculating factorials, and finding prime numbers up to a specified limit. Each program is accompanied by its code and corresponding output. These examples serve as practical applications of PL/SQL programming concepts.

Uploaded by

sopoy59260
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

PL/SQL Code Examples with Outputs

The document contains several PL/SQL programs demonstrating various functionalities, including displaying patterns, generating Fibonacci sequences, retrieving top employees by salary, calculating factorials, and finding prime numbers up to a specified limit. Each program is accompanied by its code and corresponding output. These examples serve as practical applications of PL/SQL programming concepts.

Uploaded by

sopoy59260
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PL/SQL Programs with Output

1. Display Pattern

Code:
BEGIN
FOR i IN 1..3 LOOP
FOR j IN 1..i LOOP
DBMS_OUTPUT.PUT(j || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END LOOP;

FOR i IN REVERSE 1..2 LOOP


FOR j IN 1..i LOOP
DBMS_OUTPUT.PUT(j || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END LOOP;
END;

Output:
1
1 2
1 2 3
1 2
1

2. Fibonacci Sequence

Code:
DECLARE
a NUMBER := 0;
b NUMBER := 1;
c NUMBER;
n NUMBER := 10;
BEGIN
DBMS_OUTPUT.PUT_LINE('Fibonacci Series:');
DBMS_OUTPUT.PUT_LINE(a);
DBMS_OUTPUT.PUT_LINE(b);
FOR i IN 3..n LOOP
c := a + b;
DBMS_OUTPUT.PUT_LINE(c);
a := b;
b := c;
END LOOP;
END;

Output:
Fibonacci Series:
0
PL/SQL Programs with Output

1
1
2
3
5
8
13
21
34

3. Top 8 Employees (Sample Output)

Code:
DECLARE
CURSOR top_emps IS
SELECT * FROM employees ORDER BY salary DESC FETCH FIRST 8 ROWS ONLY;
emp_row employees%ROWTYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('Top 8 Employees by Salary:');
FOR emp_row IN top_emps LOOP
DBMS_OUTPUT.PUT_LINE('ID: ' || emp_row.employee_id ||
', Name: ' || emp_row.first_name ||
', Salary: ' || emp_row.salary);
END LOOP;
END;

Output:
Top 8 Employees by Salary:
ID: 101, Name: John, Salary: 12000
... (6 more lines)
ID: 108, Name: Alice, Salary: 8900

4. Factorial of a Number

Code:
DECLARE
n NUMBER := 5;
fact NUMBER := 1;
BEGIN
FOR i IN 1..n LOOP
fact := fact * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial of ' || n || ' is ' || fact);
END;

Output:
Factorial of 5 is 120
PL/SQL Programs with Output

5. Prime Numbers up to N

Code:
DECLARE
n NUMBER := 50;
i NUMBER;
j NUMBER;
is_prime BOOLEAN;
BEGIN
DBMS_OUTPUT.PUT_LINE('Prime numbers up to ' || n || ':');
FOR i IN 2..n LOOP
is_prime := TRUE;
FOR j IN 2..TRUNC(SQRT(i)) LOOP
IF MOD(i, j) = 0 THEN
is_prime := FALSE;
EXIT;
END IF;
END LOOP;
IF is_prime THEN
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END;

Output:
Prime numbers up to 50:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

You might also like