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

Data Base Management Systems Lab-Assignment 1

The document contains multiple PL/SQL programs that demonstrate various functionalities including displaying a number pattern, generating the Fibonacci sequence, retrieving details of the top 8 employees by salary, calculating the factorial of a number, and finding all prime numbers up to a specified limit. Each program is structured with a declaration section, a main execution block, and uses DBMS_OUTPUT for displaying results. The examples illustrate basic programming constructs such as loops, conditionals, and cursors in PL/SQL.

Uploaded by

phanichowdary882
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)
45 views3 pages

Data Base Management Systems Lab-Assignment 1

The document contains multiple PL/SQL programs that demonstrate various functionalities including displaying a number pattern, generating the Fibonacci sequence, retrieving details of the top 8 employees by salary, calculating the factorial of a number, and finding all prime numbers up to a specified limit. Each program is structured with a declaration section, a main execution block, and uses DBMS_OUTPUT for displaying results. The examples illustrate basic programming constructs such as loops, conditionals, and cursors in PL/SQL.

Uploaded by

phanichowdary882
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
You are on page 1/ 3

1.

To display the given number pattern using PL/SQL

Ans:-
SET SERVEROUTPUT ON;

DECLARE
i NUMBER;
j NUMBER;
BEGIN
-- First Half: Increasing pattern
FOR i IN 1..3 LOOP
FOR j IN 1..i LOOP
DBMS_OUTPUT.PUT(j || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END LOOP;

-- Second Half: Decreasing pattern


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;
/

2. Create a PL/SQL program that generates the Fibonacci sequence.

Ans:-
SET SERVEROUTPUT ON;

DECLARE
n NUMBER := 10; -- Number of terms to generate
a NUMBER := 0;
b NUMBER := 1;
temp NUMBER;
i NUMBER := 1;
BEGIN
DBMS_OUTPUT.PUT_LINE('Fibonacci Sequence:');
WHILE i <= n LOOP
DBMS_OUTPUT.PUT_LINE(a);
temp := a + b;
a := b;
b := temp;
i := i + 1;
END LOOP;
END;
/
3. Write a PL/SQL program to display the details of top 8 employee in the company.

Ans:-
SET SERVEROUTPUT ON;

DECLARE
CURSOR top_employees_cur IS
SELECT emp_id, emp_name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 8 ROWS ONLY;

v_emp_id employees.emp_id%TYPE;
v_emp_name employees.emp_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('Top 8 Employees by Salary:');
DBMS_OUTPUT.PUT_LINE('------------------------------------------');
DBMS_OUTPUT.PUT_LINE('ID || Name || Salary');
DBMS_OUTPUT.PUT_LINE('------------------------------------------');

OPEN top_employees_cur;
LOOP
FETCH top_employees_cur INTO v_emp_id, v_emp_name, v_salary;
EXIT WHEN top_employees_cur%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(v_emp_id || ' || ' || v_emp_name || ' || ' || v_salary);


END LOOP;
CLOSE top_employees_cur;
END;
/

4. Write a PL/SQL program to calculate the factorial of a number.

Ans:-
SET SERVEROUTPUT ON;

DECLARE
num NUMBER := 5; -- Change this to calculate factorial of a different number
factorial NUMBER := 1;
i NUMBER := 1;
BEGIN
IF num < 0 THEN
DBMS_OUTPUT.PUT_LINE('Factorial is not defined for negative numbers.');
ELSIF num = 0 THEN
DBMS_OUTPUT.PUT_LINE('Factorial of 0 is 1.');
ELSE
FOR i IN 1..num LOOP
factorial := factorial * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || factorial);
END IF;
END;
/
5. Write a PL/SQL program to find all prime numbers up to a given number

Ans:-
SET SERVEROUTPUT ON;

DECLARE
max_num NUMBER := 30; -- Change this value as needed
i NUMBER;
j NUMBER;
is_prime BOOLEAN;
BEGIN
DBMS_OUTPUT.PUT_LINE('Prime numbers up to ' || max_num || ' are:');

FOR i IN 2..max_num 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;
/

You might also like