Wednesday, 20 February 2013

PLSQL BASIC PROGRAMMS

01 Write a program to find the sum of digits
declare
n number(4);
s number(2):=0;
a number; begin
while n!=0 loop
a:=n mod 10;
s:=s+a;
n:=floor(n/10); end loop;
dbms_output.put_line(' The digit sum is 'j|s); end;
Input
Enter value for n: 6789 Output
old   6: n:=&n; new   6: n:=6789; The digit sum is 30
PL SQL procedure successfully completed.

02 Write a program to reverse a number
declare
a number(5):=&a; r number; x nurnber(5); begin
x:=0:
while a>0 loop
r:=a mod 10; x:=x*10+r; a:=floor(a/10); end loop;
dbms_output.put_line(x); end;
Input
Enter value for a: 1596 Output
old   2: a number(5):=&a; new  2: anumber(5);=:1596; 6951
PL/SQL procedure successfully completed.

03 Write a program to check whether the given number is Armstrong or not
declare
a number:=&a; s number:=0; t number; x number; begin
x^a;
while a>0 loop
t:=mod(a,10);
s:=s+power(t,3);
a:=floor(a/10); end loop; if x=s then
dbms_output.put_line(' Armstrong'); else
dbmsoutput.putlineCNot Armstrong'); end if; end:
Input
Enter value for a: 153 Output
old   2: a number:=&a; new   2: a number:-153; Armstrong
PL/SQL procedure successfully completed.

04 Write a program to check the given number is Prime or not
declare
a number:=&a; i number; f number:=0; begin
for i in 2..a-l loop
if mod(a,i)=0 then
f:=l; end if;
—:-       end loop; if f=l then
dbms_output.put_line('Not Prime1); else
dbms_output.put_line(Trime'); end if; end;
Input
Enter value for a: 47 Output
old 2; a number:=&a; new 2: a number:=47; Prime
PL SQL procedure successfully completed.

PL/SQL Programs
05 Write a program to find the factorial of a number
declare
a nurnber:=&a;
i number;
fact number; begin
fact:=l;
for i in l..a
loop.."J...           fact:=fact*i;
end loop;
dbms_output.put_line('The factorial of '||a||' is "||fact); end; /
 Input
Enter value for a: 6 Output
old   2: a number\=&z: new   2: a number:=6; The factorial of 6 is 720
PL'SQL procedure successfully completed.

06 Write a program t print even numbers using for loop
declare
s number(5): n number(5); begin
n:=&n; for s in O..n loop
ifmod(s,2)=0 then
dbms_output.put_line( s); end if; end loop: end;
Input
Enter value for n: 10 Output
old   5: n:=&n:
new   5:n:=10;
0
2
4
6
8
10
PL SQL procedure successfully completed.

07 Write a program to generate Fibonacci series using while loop
declare
n number(5):=& n; a number:=0; b number:=l; i number; begin
for i in L.n loop
dbms_output.put_line(a); a:=a+b; b:=a-b; end loop; end;
Input
Enter value for n: 8 Output
old   5: n number.=&n; new   5: n number:=8; 0
1 1
2
3 5 8 13
PL SQL procedure successfully completed.

09 Write a program for function creation
create or replace function suml(a in number,b in number) return number is s number(6);
begin
s:=a+b;
return s; end:
Output
Function created.
SQL> select suml(4.8)from dual;
SUM1(4,8)
12





















No comments:

Post a Comment