0% found this document useful (0 votes)
4 views5 pages

Essential PL/SQL Code Examples

The document contains multiple PL/SQL code snippets demonstrating various programming concepts such as palindrome checking, positive/negative/zero classification, summing numbers, printing sequences, calculating factorials, grade assignment, summing digits, age categorization, skipping numbers, and generating multiplication tables. Each snippet is self-contained and showcases a specific functionality using loops and conditional statements. The use of variables and output statements is consistent throughout the examples.

Uploaded by

nijaznaushad777
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)
4 views5 pages

Essential PL/SQL Code Examples

The document contains multiple PL/SQL code snippets demonstrating various programming concepts such as palindrome checking, positive/negative/zero classification, summing numbers, printing sequences, calculating factorials, grade assignment, summing digits, age categorization, skipping numbers, and generating multiplication tables. Each snippet is self-contained and showcases a specific functionality using loops and conditional statements. The use of variables and output statements is consistent throughout the examples.

Uploaded by

nijaznaushad777
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

Palindrome Check

DECLARE
num NUMBER := #
rev NUMBER := 0;
temp NUMBER;
BEGIN
temp := num;
WHILE temp > 0 LOOP
rev := rev * 10 + MOD(temp, 10);
temp := FLOOR(temp / 10);
END LOOP;
IF num = rev THEN
DBMS_OUTPUT.PUT_LINE('Palindrome');
ELSE
DBMS_OUTPUT.PUT_LINE('Not Palindrome');
END IF;
END;

Positive / Negative / Zero

DECLARE
num NUMBER := #
BEGIN
IF num > 0 THEN
DBMS_OUTPUT.PUT_LINE('Positive');
ELSIF num < 0 THEN
DBMS_OUTPUT.PUT_LINE('Negative');
ELSE
DBMS_OUTPUT.PUT_LINE('Zero');
END IF;
END;
Sum of First 10 Numbers

DECLARE
i NUMBER := 1;
s NUMBER := 0;
BEGIN
LOOP
s := s + i;
i := i + 1;
EXIT WHEN i > 10;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum= ' || s);
END;

Print 1–10 using WHILE


DECLARE
i NUMBER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i + 1;
END LOOP;
END;

Factorial using FOR loop


DECLARE
n NUMBER := &n;
fact NUMBER := 1;
BEGIN
FOR i IN 1..n LOOP
fact := fact * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial = ' || fact);
END;
Grade Assignment
DECLARE
score NUMBER := &score;
BEGIN
IF score >= 90 THEN
DBMS_OUTPUT.PUT_LINE('Grade: A');
ELSIF score BETWEEN 80 AND 89 THEN
DBMS_OUTPUT.PUT_LINE('Grade: B');
ELSIF score BETWEEN 70 AND 79 THEN
DBMS_OUTPUT.PUT_LINE('Grade: C');
ELSE
DBMS_OUTPUT.PUT_LINE('Grade: D');
END IF;
END;

Sum of Digits

DECLARE
num NUMBER := &num;
sumd NUMBER := 0;
rem NUMBER;
BEGIN
WHILE num > 0 LOOP
rem := MOD(num,10);
sumd := sumd + rem;
num := FLOOR(num / 10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum of digits = ' || sumd);
END;
Categorize by Age

DECLARE
age NUMBER := &age;
BEGIN
IF age BETWEEN 0 AND 12 THEN
DBMS_OUTPUT.PUT_LINE('Child');
ELSIF age BETWEEN 13 AND 19 THEN
DBMS_OUTPUT.PUT_LINE('Teenager');
ELSIF age BETWEEN 20 AND 59 THEN
DBMS_OUTPUT.PUT_LINE('Adult');
ELSE
DBMS_OUTPUT.PUT_LINE('Senior');
END IF;
END;

Print Numbers 1–10 but Skip 5


BEGIN
FOR i IN 1..10 LOOP
IF i = 5 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;

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

You might also like