You are on page 1of 4

//fibonacci series

declare
a number:=0;
b number:=1;
n number;
i number;
c number:=1;
begin
dbms_output.put_line(a);
dbms_output.put_line(b);
n:=&n;
for i in 1..(n-2) loop
dbms_output.put_line(c);
a:=b;
b:=c;
c:=a+b;
end loop;
end;
***********************************
using recursive function //wrong
declare
n number;
i number;
function fib(i number) return number
is
begin
return(fib(i-1)+fib(i-2));
end;
begin
n:=&n;
for i in 1..n loop
dbms_output.put_line(fib(i));
end loop;
end;
***************************************
declare
n number;
a number:=0;
s number:=0;
c number;
begin
n:=&n;
c:=n;
loop
a:=mod(n,10);
s:=(a*a*a)+s;
n:=trunc((n/10),0);
exit when n=0;
end loop;
dbms_output.put_line(s);
if s=c then
dbms_output.put_line('armstrong');
else
dbms_output.put_line('not armstrong');
end if;
end;

********************************************
sum of n odd nos
declare
n number;
j number:=0;
s number:=0;
i number:=1;
begin
n:=&n;
loop
dbms_output.put_line(i);
s:=s+i;
i:=i+2;
j:=j+1;
exit when j=n;
end loop;
dbms_output.put_line('sum of '||n||' odd nos is '||s);
end;
*********************************************************
sum of n even nos
declare
n number;
j number:=0;
s number:=0;
i number:=0;
begin
n:=&n;
loop
dbms_output.put_line(i);
s:=s+i;
i:=i+2;
j:=j+1;
exit when j=n;
end loop;
dbms_output.put_line('sum of '||n||' even nos is '||s);
end;
*************************************************************
count no of digits in a number::::::
declare
n number;
i number:=0;
begin
n:=&n;
loop
i:=i+1;
n:=trunc((n/10),0);
exit when n=0;
end loop;
dbms_output.put_line('no of digits is '||i);
end;
**********************************************
reverse of no::::::
declare
n number;
a number;
begin

loop
a:=mod(n,10);
s:=s+a*100;
**********************************************************
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cursors::::::
declare
v_name test1.name%type;
v_age test1.age%type;
cursor c1 is select * from test1;
begin
open c1;
for i in 1..5 loop
fetch c1 into v_name,v_age;
dbms_output.put_line(v_name||v_age);
end loop;
close c1;
end;
********************************************
declare
cursor c2 is select name,age from test1;
v_name test1.name%type;
v_age test1.age%type;
begin
open c2;
for i in 1..5 loop
fetch c2 into v_name,v_age;
exit when c2%notfound;
dbms_output.put_line(v_name);
end loop;
close c2;
end;
*******************************************
declare
cursor c1 is select name,age from test1;
c2 test1%rowtype;
begin
open c1;
for i in 1..5 loop
fetch c1 into c2;
exit when c1%notfound;
insert into test2(name,age) values(c2.name,c2.age);
end loop;
commit;
close c1;
end;
*******************************************************
declare
cursor c1 is select * from test1;
cursor c2 is select * from test2;
x test1%rowtype;
y test2%rowtype;
begin
open c1;
loop

fetch c1 into x;
exit when c1%notfound;
dbms_output.put_line(x.name||' '||x.age);
end loop;
close c1;
open c2;
loop
fetch c2 into y;
exit when c2%notfound;
dbms_output.put_line(y.name||' '||y.age);
end loop;
close c2;
end;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
factorial
declare
n1 number;
t1 number;
n2 number;
fact number;
result number;
function factorial(n1 number) return number
is
begin
t1:=n1;
fact:=1;
while t1>1 loop
fact:=fact*t1;
t1:=t1-1;
end loop;
return fact;
end;
begin
n2:=&n2;
result:=factorial(n2);
dbms_output.put_line('factorial of '||n2||' is '||result);
end;
**************************************************************
check prime:::
declare
n number;
f number:=0;
c number;
begin
n:=&n;
for i in 2..n-1 loop
c:=mod(n,i);
if (c=0) then
f:=1;
dbms_output.put_line('not prime');
exit;
end if;
end loop;
if (f=0) then
dbms_output.put_line('prime');
end if;
end;

You might also like