0% found this document useful (0 votes)
6 views8 pages

PL/SQL Loop Exercises and Solutions

The document contains a series of PL/SQL programming exercises that demonstrate various control structures such as LOOP, WHILE, and FOR loops. Each exercise includes a code block that performs specific tasks like printing natural numbers, calculating factorials, generating Fibonacci series, and checking for perfect or Armstrong numbers. The exercises are designed for practical learning and include user input for flexibility.

Uploaded by

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

PL/SQL Loop Exercises and Solutions

The document contains a series of PL/SQL programming exercises that demonstrate various control structures such as LOOP, WHILE, and FOR loops. Each exercise includes a code block that performs specific tasks like printing natural numbers, calculating factorials, generating Fibonacci series, and checking for perfect or Armstrong numbers. The exercises are designed for practical learning and include user input for flexibility.

Uploaded by

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

PLSQL PRACTICAL 3

DATE: 08/12/2025 PRATHAM NILESH CHUDASAMA ROLL NO. KCSYCS09

1. Write a PL/SQL block using basic LOOP to print the first 10 natural numbers.

DECLARE
i NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i + 1;
EXIT WHEN i > 10;
END LOOP;
END;
/
OUTPUT:

2. Write a PL/SQL block using basic LOOP to print the first n natural numbers (value of
n from user).

DECLARE
n NUMBER := &n;
i NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i + 1;
EXIT WHEN i > n;
END LOOP;
END;
/
OUTPUT:

[Link] a PL/SQL block using basic LOOP to print the squares of numbers from 1 to n.

DECLARE
n NUMBER := &n;
i NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(i * i);
i := i + 1;
EXIT WHEN i > n;
END LOOP;
END;
/
Output:
4. Write a PL/SQL block using WHILE loop to print the sum of the first n natural numbers.

DECLARE
n NUMBER := &n;
i NUMBER := 1;
sum NUMBER := 0;
BEGIN
WHILE i <= n LOOP
sum := sum + i;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum: ' || sum);
END;
/
Output:

5. Write a PL/SQL block using WHILE loop to calculate the factorial of a given number.

DECLARE
n NUMBER := &n;
i NUMBER := 1;
fact NUMBER := 1;
BEGIN
WHILE i <= n LOOP
fact := fact * i;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial = ' || fact);
END;
/
Output:

[Link] a PL/SQL block using WHILE loop to print the Fibonacci series up to n terms.

DECLARE
n NUMBER := &n;
i NUMBER := 1;
a NUMBER := 0;
b NUMBER := 1;
c NUMBER;
BEGIN
WHILE i <= n LOOP
DBMS_OUTPUT.PUT_LINE(a);
c := a + b;
a := b;
b := c;
i := i + 1;
END LOOP;
END;
/
Output:

7. Write a PL/SQL block using WHILE loop to check whether a given number is perfect
or not.

DECLARE
n NUMBER := &n;
i NUMBER := 1;
sum NUMBER := 0;
BEGIN
WHILE i < n LOOP
IF MOD(n, i) = 0 THEN
sum := sum + i;
END IF;
i := i + 1;
END LOOP;

IF sum = n THEN
DBMS_OUTPUT.PUT_LINE(n || ' is a Perfect Number');
ELSE
DBMS_OUTPUT.PUT_LINE(n || ' is Not a Perfect Number');
END IF;
END;
/
Output:
8. Write a PL/SQL block using FOR loop to print all numbers from 1 to n.

DECLARE
n NUMBER := &n;
BEGIN
FOR i IN 1..n LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/
Output:

9. Write a PL/SQL block using REVERSE FOR loop to print numbers from n down to 1.

DECLARE
n NUMBER := &n;
BEGIN
FOR i IN REVERSE 1..n LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/
Output:

[Link] a PL/SQL block using FOR loop to print the multiplication table of a given number.

DECLARE
n NUMBER := &n;
BEGIN
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(n || ' x ' || i || ' = ' || n * i);
END LOOP;
END;
/
Output:

11. Write a PL/SQL block using FOR loop to print the prime numbers from 1 to n.

DECLARE
n NUMBER := &n;
count1 NUMBER;
BEGIN
FOR i IN 2..n LOOP
count1 := 0;
FOR j IN 2..FLOOR(SQRT(i)) LOOP
IF MOD(i, j) = 0 THEN
count1 := count1 + 1;
EXIT; -- Exit early when divisor found
END IF;
END LOOP;
IF count1 = 0 THEN
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END;
/
Output:

12. Write a PL/SQL block using FOR loop to check whether a number is Armstrong or not.

DECLARE
n NUMBER := &n;
temp NUMBER;
sum NUMBER := 0;
digit NUMBER;
len NUMBER;
BEGIN
temp := n;
len := LENGTH(TO_CHAR(n));

FOR i IN 1..len LOOP


digit := MOD(temp, 10);
sum := sum + POWER(digit, len);
temp := FLOOR(temp / 10);
END LOOP;

IF sum = n THEN
DBMS_OUTPUT.PUT_LINE(n || ' is an Armstrong Number');
ELSE
DBMS_OUTPUT.PUT_LINE(n || ' is Not an Armstrong Number');
END IF;
END;
/
Output:

13. Write a PL/SQL block using nested FOR loops to print a right-angled triangle of stars (*) of size n.

DECLARE
n NUMBER := &n;
line VARCHAR2(100);
BEGIN
FOR i IN 1..n LOOP
line := '';
FOR j IN 1..i LOOP
line := line || '*';
END LOOP;
DBMS_OUTPUT.PUT_LINE(line);
END LOOP;
END;
/
Output:

14. Write a PL/SQL block using nested FOR loops to print a reverse right-angled triangle of stars of size n.

DECLARE
n NUMBER := &n;
line VARCHAR2(100);
BEGIN
FOR i IN REVERSE 1..n LOOP
line := '';
FOR j IN 1..i LOOP
line := line || '*';
END LOOP;
DBMS_OUTPUT.PUT_LINE(line);
END LOOP;
END;
/

Output:
15. Write a PL/SQL block using nested FOR loops to print multiplication tables from 1 to n.

DECLARE
n NUMBER := &n;
BEGIN
FOR i IN 1..n LOOP
DBMS_OUTPUT.PUT_LINE('Table of ' || i);
FOR j IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(i || ' x ' || j || ' = ' || i * j);
END LOOP;
DBMS_OUTPUT.PUT_LINE('---');
END LOOP;
END;
/
Output:

16. Write a PL/SQL block using nested FOR loops to print a pattern where each row contains numbers from 1 to row
number (Example for n=4):
1
12
123
1234

DECLARE
n NUMBER := &n;
line VARCHAR2(100);
BEGIN
FOR i IN 1..n LOOP
line := '';
FOR j IN 1..i LOOP
line := line || j || ' ';
END LOOP;
DBMS_OUTPUT.PUT_LINE(line);
END LOOP;
END;
/
Output:

You might also like