PLSQL Programs
PLSQL Programs
PROGRAMS
PL/SQL PROGRAMS
1. ADDITION OF TWO NUMBERS
AIM:
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> /
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> /
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;
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:
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> /
Fibonacci series is :
0
1
1
2
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:
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> /
RESULT:
Thus, the PL/SQL program to find the sum of series was executed and verified
successfully.
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> /
RESULT:
Thus, the PL/SQL program to update the values in the existing table was executed and
verified successfully.