1.Write a program to print Hello World.
SQL> begin
2 dbms_output.put_line('Hello World');
3 End;
4 /
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> /
Hello World
PL/SQL procedure successfully completed.
2. Write a program to find sum of two numbers from 1 to 100
DECLARE
N NUMBER(3):=1;
S NUMBER(4):=0;
BEGIN
WHILE N<=100
LOOP
S := S+N;
N :=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE SUM IS '||S);
END;
Output:-
The sum of Two Numbers is 15
3. Print Fibonnaci series using for loop.
declare
a number(3) := 0;
b number(3) :=1;
c number(3) :=0;
begin
dbms_output.put_line('Fibonacci series are upto 10 ');
for m in 1..10
loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end;
Example 4:- Write a pl sql function for calculate a factorial of given number.
A Recursive function is one that calls itself.Each recursive creates a new instance of any
items declared in the subprogram.
Example:-
create or replace function fac_number(num number)
return number as
begin
if num=1 then
return 1;
else
return(num*fac_number(num-1));
end if;
end;
/
SQL> select fac_number(5) from dual;
FAC_NUMBER(5)
-------------
120
Example 5:
--WRITE A PROGRAM TO PRINT EVEN NUMBERS FROM 1 TO 100
DECLARE
N NUMBER(3) :=0;
BEGIN
WHILE N<=100
LOOP
N :=N+2;
DBMS_OUTPUT.PUT_LINE(N);
END LOOP;
END;
Example 6 .--Write a program to calculate the sum of 10 natural numbers.
Declare
a number;
tot number default 0;
Begin
a:=1;
loop
tot:=tot+a;
exit when (a=10);
a:=a+1;
end loop;
dbms_output.put_line('Sum between 1 to 100 is '||tot);
End;
/
Example 7 .Write a program to accept a number and print it in reverse order
DECLARE
x NUMBER(5):=&x;
REV NUMBER(5):=0;
R NUMBER(5):=0;
BEGIN
WHILE x !=0
LOOP
R:=MOD(x,10);
REV:=REV*10+R;
x:=TRUNC(x/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE REVERSE OF A GIVEN NUMBER IS '||REV);
END;
Example 8:-
SQL> begin
2 dbms_output.put_line('Hello World');
3 End;
4 /
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> /
Hello World
PL/SQL procedure successfully completed.
2. Write a program to find sum of two numbers from 1 to 100
DECLARE
N NUMBER(3):=1;
S NUMBER(4):=0;
BEGIN
WHILE N<=100
LOOP
S := S+N;
N :=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE SUM IS '||S);
END;
Output:-
The sum of Two Numbers is 15
3. Print Fibonnaci series using for loop.
declare
a number(3) := 0;
b number(3) :=1;
c number(3) :=0;
begin
dbms_output.put_line('Fibonacci series are upto 10 ');
for m in 1..10
loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end;
Example 4:- Write a pl sql function for calculate a factorial of given number.
A Recursive function is one that calls itself.Each recursive creates a new instance of any
items declared in the subprogram.
Example:-
create or replace function fac_number(num number)
return number as
begin
if num=1 then
return 1;
else
return(num*fac_number(num-1));
end if;
end;
/
SQL> select fac_number(5) from dual;
FAC_NUMBER(5)
-------------
120
Example 5:
--WRITE A PROGRAM TO PRINT EVEN NUMBERS FROM 1 TO 100
DECLARE
N NUMBER(3) :=0;
BEGIN
WHILE N<=100
LOOP
N :=N+2;
DBMS_OUTPUT.PUT_LINE(N);
END LOOP;
END;
Example 6 .--Write a program to calculate the sum of 10 natural numbers.
Declare
a number;
tot number default 0;
Begin
a:=1;
loop
tot:=tot+a;
exit when (a=10);
a:=a+1;
end loop;
dbms_output.put_line('Sum between 1 to 100 is '||tot);
End;
/
DECLARE
x NUMBER(5):=&x;
REV NUMBER(5):=0;
R NUMBER(5):=0;
BEGIN
WHILE x !=0
LOOP
R:=MOD(x,10);
REV:=REV*10+R;
x:=TRUNC(x/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('THE REVERSE OF A GIVEN NUMBER IS '||REV);
END;
Example 8:-