Wednesday, November 16, 2011

PLSQL Example Find Even Odd Number

It is a sample plsql example code to find accepted number is even or odd and print output with friendly message.

Even or Odd Number
Even number is a number which is completely divided by 2 and reminder is 0 like 2, 4, 6, 8, 10, ..
Odd number is a number which is completely not divided by 2 and reminder is not 0 like 1, 3, 5, 7, 9, ..


Aim - PLSQL Example to find accepted number is even or odd


Program Code : -

declare
  num number(5);
begin
  num:=#
  if num mod 2 =0 then
    dbms_output.put_line('Accepted Number '||num||' is even');
  else
    dbms_output.put_line('Accepted Number '||num||' is odd');
  end if;
end;



Output of PLSQL Block:-
Enter value for num: 10
old 5: num:=&num
new 5: num:=10;
Accepted Number 10 is even


Saturday, November 12, 2011

PL/SQL Example to find average of three numbers

This is a PL/SQL sample example to accept three numbers from the user and find average of accepted numbers.

Aim - PL/SQL Example to find average of three numbers



Sample Program : -


declare
a number(3);
b number(3);
c number(3);
avgr number(5,2);
begin
a:=&a;
b:=&b;
c:=&c;
avgr:=((a+b+c)/3);
dbms_output.put_line('Average of a, b, c is: '||avgr);
end;


Output of PL SQL Block: -

Enter value for a: 22
old 7: a:=&a;
new 7: a:=22;
Enter value for b: 32
old 8: b:=&b;
new 8: b:=32;
Enter value for c: 43
old 9: c:=&c;
new 9: c:=43;
Average of a, b, c is: 32.33

PL/SQL program to generate Fibonacci series

It is a sample example of generate fibonacci series upto 10 numbers.

Aim - PL/SQL program to generate Fibonacci series.



PLSQL Sample Program: -

declare
f1 number(3);
f2 number(3);
f3 number(3);
num number(3);
begin
f1:=0;
f2:=1;
f3:=0;
num:=1;
while num<=10
loop
dbms_output.put_line(f3);
f1 :=f2;
f2:=f3;
f3:=f1+f2;
num:=num+1;
end loop;
end;




Output of PL SQL Block: -
0
1
1
2
3
5
8
13
21
34

PL/SQL Program to reverse a string

This is a sample PLSQL example to accept a string from user and print reverse of accept string.

Aim - PL/SQL Program to reverse a string.



Sample PLSQL Program: -
declare
str1 varchar2(30);
len number(3);
str2 varchar2(30);
i number(3);
begin
str1:='&str1';
len:=length(str1);
for i in reverse 1..len
loop
str2:=str2 || substr(str1,i,1);
end loop;
dbms_output.put_line('Reverse string is: '||str2);
end;




Output of PL SQL Block: -
Enter value for str1: hello
old 6: str1:='&str1';
new 6: str1:='hello';
Reverse string is: olleh