You are on page 1of 8

PL/SQL

PROGRAMS
PL/SQL PROGRAMS
1. ADDITION OF TWO NUMBERS

AIM:

To write the PL/SQL program to find the addition of two numbers.

PROGRAM CODE:

declare
x integer;
y integer;
z integer;
begin
x:=&x;
y:=&y;
z:=x+y;
dbms_output.put_line('The Sum of two number is = '|| z);
end;

OUTPUT:

SQL> /

SQL> set serveroutput on;

Enter value for x: 2


old 6: x:=&x;
new 6: x:=2;

Enter value for y: 3


old 7: y:=&y;
new 7: y:=3;

The Sum of two number is = 5

PL/SQL procedure successfully completed.

RESULT:

Thus, the PL/SQL program to find the addition of two numbers was executed and
verified successfully.
2. FACTORIAL OF A NUMBER

AIM:

To write the PL/SQL program to find the Factorial of the given number.

PROGRAM CODE:

declare
f number:=1;
i number;
n number:=&n;
begin
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line('The Factorial of a given no is : '||f);
end;

OUTPUT:

SQL> /

SQL> set serveroutput on;

Enter value for n: 4


old 4: n number:=&n;
new 4: n number:=4;

The Factorial of a given no is : 24

PL/SQL procedure successfully completed.

RESULT:

Thus, the PL/SQL program to find the factorial of a number was executed and
verified successfully.
3. PALINDROME CHECKING

AIM:

To write the PL/SQL program to check the given string is a Palindrome or not.

PROGRAM CODE:

declare
len number;
a integer;
str1 varchar(10):='&str1';
str2 varchar(10);
begin
len:=length(str1);
a:=len;
for i in 1..a
loop
str2:=str2||substr(str1,len,1);
len:=len-1;
end loop;
if(str1=str2) then
dbms_output.put_line(str1||' is a palindrome');
else
dbms_output.put_line(str1||' is not a palindrome');
end if;
end;

OUTPUT:

SQL> /
SQL> set serveroutput on;

Enter value for str1: MADAM


old 4: str1 varchar(10):='&str1';
new 4: str1 varchar(10):='MADAM';
MADAM is a palindrome

Enter value for str1: ARUN


old 4: str1 varchar(10):='&str1';
new 4: str1 varchar(10):='ARUN';
ARUN is not a palindrome

PL/SQL procedure successfully completed.

RESULT:
Thus, the PL/SQL program to check the given string is a palindrome or not was
executed and verified successfully.

4. FIBONACCI SERIES

AIM:

To write the PL/SQL program to print the Fibonacci series.

PROGRAM CODE:

declare
i number;
c number;
n number:=&n;
a number:=-1;
b number:=1;
begin
dbms_output.put_line(`Fibonacci series is : `);
for i in 1..n
loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end;

OUTPUT:

SQL> /

SQL> set serveroutput on;

Enter value for n: 4


old 4: n number:=&n;
new 4: n number:=4;

Fibonacci series is :
0
1
1
2

PL/SQL procedure successfully completed.

RESULT:
Thus, the PL/SQL program to print the Fibonacci series of the given number was
executed and verified successfully.

5. SUM OF SERIES

AIM:

To write the PL/SQL program to find the Sum of series.

PROGRAM CODE:

declare
i number;
n number:=&n;
begin
i:=n*(n+1);
n:=i/2;
dbms_output.put_line('The sum of series is : '||n);
end;
(OR)
declare
i number;
n number:=&n;
s number:=0;
begin
for i in 1..n
loop
s:=s+i;
end loop;
dbms_output.put_line('The sum of Series is : '||s);
end;

OUTPUT:

SQL> /

SQL> set serveroutput on;

Enter value for n: 10


old 3: n number:=&n;
new 3: n number:=10;

The sum of series is : 55

PL/SQL procedure successfully completed.

RESULT:
Thus, the PL/SQL program to find the sum of series was executed and verified
successfully.

UPDATING THE STUDENT GRADE


AIM:

To write the PL/SQL program to update the values in the existing table.

PROGRAM CODE:

declare
rn number:=&rn;
grad char(3);
tot number;
begin
select total into tot from student where rollno=rn;
if tot>150 then
grad:='S';
elsif tot<150 and tot>120 then
grad:='A';
elsif tot<120 and tot>100 then
grad:='B';
else
grad:='C';
end if;
update student set grade=grad where rollno=rn;
end;

OUTPUT:

SQL> /

SQL> set serveroutput on;

SQL> select * from student;

ROLLNO NAME MARK1 MARK2 TOTAL GRA


------------- ---------- ----------- ----------- ---------- -------
101 Arun 100 98 198
102 Mani 95 80 175
103 Ganesan 35 75 110

Enter value for rn: 102


old 2: rn number:=&rn;
new 2: rn number:=102;
PL/SQL procedure successfully completed.

SQL> select * from student;

ROLLNO NAME MARK1 MARK2 TOTAL GRA


------------- ---------- ----------- ----------- ---------- -------
101 Arun 100 98 198
102 Mani 95 80 175 S
103 Ganesan 35 75 110

RESULT:

Thus, the PL/SQL program to update the values in the existing table was executed and
verified successfully.

You might also like