Friday 13 November 2015

pl sql program for 'palindrome'

Write a program in PL/SQL to check whether a number is a palindrome or not.

In this article, we will see the program for Palindrome in Pl/Sql.

palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers.

Let's see the program for this.


sql> set serveroutput on
sql>declare
2  g varchar2(20);
3  r varchar2(20);
4  i number(4);
5  begin
6  g:='&g';
7  for i in reverse 1.. length(g) loop
8  r:=r || substr(g,i,1);      
9  end loop;
10  dbms_output.put_line('reverse string is ' || r);
11  if r=g then
12  dbms_output.put_line('String is Palindrome');
13  else
14  dbms_output.put_line('String is not Palindrome');
15  end if;      
16  end;
17  /


Thanks.

Saturday 7 November 2015

Reverse Of a Number

Write a program in PL/SQL to print the reverse of a number.

According to the problem if a number  is 677 then it should print 776.

Program Code:

SQL> set serveroutput on
SQL>Declare
2 num number:=&n;
3 rev number:=0;
4 Begin
5 while(num>0)
6 Loop
7 rev:=rev*10+mod(num,10);
8 num:=num/10;
9 End loop;
10 Dbms_Output.Put_Line('Reverse of number is '|| rev);
11 End;
12 /



Thanks.

PL/SQL Fibonacci Sequence

Write a PL/SQL code to get the Fibonacci Sequence.

First, I will explain what is Fibonacci Sequence and how to get this series.
So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,13,21.............

In Fibonacci Sequence, first and second elements are 0 and 1 and to get the next elements we will add the previous elements and it will generate the next element.
So, first element=0
      second element =1
      third element=sum of last 2 elements (first element + second element)
                           =0+1
                           =1
      fourth element=second element + third element
                             =1+1
                             =2
      fifth element=third element + fourth element
                           =1+2
                           =3

So in this way we can generate a Fibonacci Sequence.

Program Code:

SQL> declare
  2  a number(5);
  3  b number(5);
  4  c number(5);
  5  n number(5);
  6  i number(5);
  7  begin
  8  n:=6;
  9  a:=0;
 10  b:=1;
 11  for i in 1..n
 12  loop
 13  c:=a+b;
 14  a:=b;
 15  b:=c;
 16   dbms_output.put_line(c);
 17  end loop;
 18  end;

 19  /

Thanks.

PL/SQL factorial program

Write a program in PL/SQL to print the factorial of a number.



In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find the factorial of a number.
To get the factorial of any given number we have to multiply that number to every number less than that number till 1.
Suppose we have to find the factorial of 5. Then,
factorial(5)=5*4*3*2*1
                  =120


Program Code:

SQL> set serveroutput on
SQL> declare
  2  i number(5);
  3  j number (5);
  4  f number(10);
  5  begin
  6  i:=&i;
  7  f:=1;
  8  for j in 1..i
  9  loop
 10  f:=f*j;
 11  end loop;
 12  dbms_output.put_line(f);
 13  end;
 14  /

Thanks