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

Correct Extra PLSQL Programs

The document contains a series of PL/SQL programs that perform various mathematical and logical operations, including summing two numbers, calculating factorials, checking voting eligibility, generating Fibonacci series, finding the greatest of three numbers, swapping two numbers, reversing a number, summing digits, checking for prime numbers, generating multiplication tables, and verifying Armstrong numbers. Each program is structured with a declaration section, a begin-end block, and outputs results using DBMS_OUTPUT. The programs utilize substitution variables for runtime input.

Uploaded by

venkatlakshana79
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)
6 views21 pages

Correct Extra PLSQL Programs

The document contains a series of PL/SQL programs that perform various mathematical and logical operations, including summing two numbers, calculating factorials, checking voting eligibility, generating Fibonacci series, finding the greatest of three numbers, swapping two numbers, reversing a number, summing digits, checking for prime numbers, generating multiplication tables, and verifying Armstrong numbers. Each program is structured with a declaration section, a begin-end block, and outputs results using DBMS_OUTPUT. The programs utilize substitution variables for runtime input.

Uploaded by

venkatlakshana79
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

1.

SUM OF 2 NUMBERS PROGRAM

SET SERVEROUTPUT ON;

DECLARE

num1 NUMBER;

num2 NUMBER;

sum_result NUMBER;

BEGIN

num1:=&num1;

num2:=&num2;

sum_result := num1 + num2;

DBMS_OUTPUT.PUT_LINE('Sum of ' || num1 || ' and


' || num2 || ' is: ' 

|| sum_result);

END;

/
2. FACTORIAL USING LOOP

SET SERVEROUTPUT ON;

DECLARE

n NUMBER;

fact NUMBER := 1;

BEGIN

n:=&n;

FOR i IN 1..n LOOP

fact := fact * i;

END LOOP;

DBMS_OUTPUT.PUT_LINE('Factorial of ' || n || ' is: '


|| fact);

END;

/
3. PROGRAM TO CHECK VOTING ELIGIBILITY
BASED ON AGE

SET SERVEROUTPUT ON;

DECLARE

age NUMBER; 

BEGIN

age := &Enter_Age; -- Substitution variable for


runtime input

IF age < 0 THEN

DBMS_OUTPUT.PUT_LINE('Error: Age cannot


be negative.');

ELSIF age >= 18 THEN

DBMS_OUTPUT.PUT_LINE('You are eligible to


vote.');
ELSE

DBMS_OUTPUT.PUT_LINE('You are NOT


eligible to vote.');

END IF;

End;

/
4. FIBONACCI SERIES

SET SERVEROUTPUT ON;

DECLARE

n NUMBER; 

a NUMBER := 0;

b NUMBER := 1;

c NUMBER;

i NUMBER := 1;

BEGIN

n := &n;

DBMS_OUTPUT.PUT_LINE('Fibonacci Series up to '


|| n || ' terms:');

WHILE i <= n LOOP

DBMS_OUTPUT.PUT_LINE(a);
c := a + b;

a := b;

b := c;

i := i + 1;
END LOOP;

END;

/

5. GREATEST OF THREE NUMBERS 

DECLARE

a NUMBER := &num1;

b NUMBER := &num2;

c NUMBER := &num3;

greatest NUMBER;

BEGIN
IF a > b AND a > c THEN

greatest := a;

ELSIF b > c THEN

greatest := b;

ELSE

greatest := c;

END IF;

DBMS_OUTPUT.PUT_LINE('Greatest of the three


numbers is: ' || greatest);

END;

/

OUTPUT
Enter value for num1: 29

old 2: a NUMBER := &num1;

new 2: a NUMBER := 29;


Enter value for num2: 18

old 3: b NUMBER := &num2;

new 3: b NUMBER := 18;

Enter value for num3: 74

old 4: c NUMBER := &num3;

new 4: c NUMBER := 74;

Greatest of the three numbers is: 74


6. PROGRAM TO SWAP TWO NUMBERS

DECLARE

num1 NUMBER;

num2 NUMBER;

BEGIN

num1 := &num1;

num2 := &num2;

DBMS_OUTPUT.PUT_LINE('Before Swapping');

DBMS_OUTPUT.PUT_LINE('Number 1 = ' || num1);

DBMS_OUTPUT.PUT_LINE('Number 2 = ' || num2);

num1 := num1 + num2;

num2 := num1 - num2;

num1 := num1 - num2;

DBMS_OUTPUT.PUT_LINE('After Swapping');
DBMS_OUTPUT.PUT_LINE('Number 1 = ' || num1);

DBMS_OUTPUT.PUT_LINE('Number 2 = ' || num2);

END;

/

OUTPUT
Enter value for num1: 29

old 5: num1 := &num1;

new 5: num1 := 29;

Enter value for num2: 18

old 6: num2 := &num2;

new 6: num2 := 18;

Before Swapping

Number 1 = 29

Number 2 = 18
After Swapping

Number 1 = 18

Number 2 = 29
7. REVERSE A NUMBER

DECLARE

n NUMBER; 

rev NUMBER := 0;

rem NUMBER; 

BEGIN

n := &n;

WHILE n > 0 LOOP

rem := MOD(n, 10);

rev := (rev * 10) + rem;

n := TRUNC(n / 10);

END LOOP;

DBMS_OUTPUT.PUT_LINE('Reversed Number is: '


|| rev);
END;

/

OUTPUT
Enter value for n: 12345
old 6: n := &n;
new 6: n := 12345;
Reversed Number is: 54321
8. SUM OF DIGITS OF A NUMBER

DECLARE

num NUMBER;

temp NUMBER;

digit NUMBER;

sum_digits NUMBER := 0;

BEGIN

num := &num;

temp := num;

WHILE temp > 0 LOOP

digit := MOD(temp, 10);

sum_digits := sum_digits + digit;

temp := TRUNC(temp / 10);


END LOOP;

DBMS_OUTPUT.PUT_LINE('Sum of digits of ' ||


num || ' is: ' || sum_digits);

END;

/

OUTPUT:
Enter value for num: 18
old 7: num := &num;
new 7: num := 18;
Sum of digits of 18 is:9


9. PROGRAM TO CHECK PRIME NUMBER 
SET SERVEROUTPUT ON;

DECLARE
n NUMBER := &num; 
i NUMBER;
flag NUMBER := 0;
BEGIN
IF n <= 1 THEN
DBMS_OUTPUT.PUT_LINE(n || ' is NOT a Prime Number');
ELSE
FOR i IN 2 .. (n/2) LOOP
IF MOD(n, i) = 0 THEN
flag := 1;
EXIT;
END IF;
END LOOP;

IF flag = 0 THEN
DBMS_OUTPUT.PUT_LINE(n || ' is a Prime Number');
ELSE
DBMS_OUTPUT.PUT_LINE(n || ' is NOT a Prime Number');
END IF;
END IF;
END;
/

Enter value for num: 7


old 2: n NUMBER := &num;
new 2: n NUMBER := 7;
7 is a Prime Number
10. PROGRAM TO GENERATE MULTIPLICATION TABLE
SET SERVEROUTPUT ON;

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

Enter value for num: 5


old 2: n NUMBER := &num;
new 2: n NUMBER := 5;
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
11. PROGRAM TO CHECK ARMSTRONG NUMBER
SET SERVEROUTPUT ON;

DECLARE
n NUMBER := &num;
temp NUMBER := n;
sum NUMBER := 0;
rem NUMBER;
BEGIN
WHILE n > 0 LOOP
rem := MOD(n, 10);
sum := sum + POWER(rem, 3);
n := FLOOR(n / 10);
END LOOP;

IF sum = temp THEN


DBMS_OUTPUT.PUT_LINE('Armstrong Number');
ELSE
DBMS_OUTPUT.PUT_LINE('Not an Armstrong Number');
END IF;
END;
/

DECLARE
n NUMBER := &num;
temp NUMBER :=n;
sum_val NUMBER := 0;
rem NUMBER;
BEGIN
WHILE temp > 0 LOOP
rem := MOD(temp, 10);
sum_val := sum_val + POWER(rem, 3);
temp :=FLOOR(temp / 10);
END LOOP;

IF sum_val = n THEN
DBMS_OUTPUT.PUT_LINE('Armstrong Number');
ELSE
DBMS_OUTPUT.PUT_LINE('Not Armstrong Number');
END IF;
END;
/

You might also like