Solved Basic PL/SQL Programs


Here is the list of some solved basic PL/SQL programs examples. These programs helps you to learn branching and looping concepts. These programs does not involve the use of any database. They are best suited for beginners to learn basic of PL/SQL programming.

Write a program to read two numbers from the user and find the biggest number from the two numbers.

 declare

            No1 number;

            No2 number;

begin

            No1:=&number;

            No2:=&number;

            if no1>no2 then

                        dbms_output.put_line('No1 is greater');

            else

                        dbms_output.put_line('No2 is greater');

            end if;

end;

 Output:

SQL > Enter the value of No1: 12

SQL > Enter the value of No2: 14

SQL > No2 is greater

 Write a program to read three numbers from the user and find the biggest number from the three numbers.

 declare

            n1 number;

            n2 number;

            n3 number;

begin

            n1:=&n1;

            n2:=&n2;

            n3:=&n3;

            if((n1>n2) and (n1>n3)) then

                        dbms_output.put_line('No1 is greater: ' || n1);

            elsif((n2>n1) and (n2>n3)) then

                        dbms_output.put_line('No2 is greater: ' || n2);

            else

                        dbms_output.put_line('No3 is greater: ' || n3);

            end if;

end;

 Output:

SQL> /

Enter value for n1: 7

Enter value for n2: 2

Enter value for n3: 1

No1 is greater: 7

 Write a program to read a number from user and determine whether it is odd or even.

 declare

            no number;

begin

            no:=&no;

            if MOD(no,2) = 0 then

                        dbms_output.put_line("given number " || no || "is EVEN");

           else

                        dbms_output.put_line("given number " || no || "is ODD");

           end if;

end;

 Output:

SQL > Enter the value of No: 12

SQL > Given number 12 is EVEN

 Write a program swap or exchange values of two variables with using third variables

declare

num1 number:=&num1;

            num2 number:=&num2;

            temp number;

begin

            dbms_output.put_line('Before exchange');

            dbms_output.put_line('number 1 is : ' || num1);

            dbms_output.put_line('number 2 is : ' || num2);

                        temp := num1;

                        num1 := num2;

                        num2 := temp;

            dbms_output.put_line('after exchange');

            dbms_output.put_line('number 1 is : ' || num1);

            dbms_output.put_line('number 2 is : ' || num2);

end;

 Output:

SQL >

Enter value for num1: 2

Enter value for num2: 5

Before exchange

number 1 is : 2

number 2 is : 5

after exchange

number 1 is : 5

number 2 is : 2

 Write a program swap values of two variables without using third variables

 declare

            num1 number:=&num1;

            num2 number:=&num2;

begin

            dbms_output.put_line('Before exchange');

            dbms_output.put_line('number 1 is : ' || num1);

            dbms_output.put_line('number 2 is : ' || num2);

                        num1 := num1 + num2;

                        num2 := num1 - num2;

                        num1 := num1 - num2;

            dbms_output.put_line('after exchange');

            dbms_output.put_line('number 1 is : ' || num1);

            dbms_output.put_line('number 2 is : ' || num2);

end;

  Write a program to print odd numbers

 declare

            i number:=1;

            no number := &no;

            sum number := 0;

begin

            while (i <= no)

            loop

            dbms_output.put_line('I is : ' || i);

            i:= i + 2;

            end loop;

end;

 Output

SQL> Enter the value for no: 7

SQL > I is :1

SQL > I is : 3

SQL > I is : 5

 Write a program to print even numbers

declare

            i number:=2;

            no number := &no;

begin

            while (i <= no)

            loop

                        dbms_output.put_line('I is : ' || i);

                        i:= i + 2;

            end loop;

end;

 Output:

SQL> /

Enter value for no: 10

I is : 2

I is : 4

I is : 6

I is : 8

I is : 10

 Write a program to print 1,2,4,8,16

declare

            i number:=1;

            no number := &no;

begin

            while (i <= no)

            loop

                        dbms_output.put_line('I is : ' || i);

                        i:= i * 2;

            end loop;

end;

 

Output

Enter value for no: 10

I is : 1

I is : 2

I is : 4

I is : 8

 Write a program to print from user given number to 1.

declare

            num1 number:= &num1;

begin

            for i in reverse 1..num1

            loop

                        dbms_output.put_line(i);

            end loop;

end;

Output

Enter value for num1: 5

5

4

3

2

1

 Write a program to print 1, 12, 123, 1234, 12345.

 begin

            for i in 1..5

            loop

                        for j in 1..i

                        loop

                                    dbms_output.put(j);

                        end loop;

            dbms_output.put_line('');

            end loop;

 end;  

 Output:

1

12

123

1234

12345

 Write a program to check whether given number is prime or not

 declare

            no number:= &no;

            flag number := 0;

begin

            for i in 2..no

            loop

                        if (mod(no,i)=0) then

                                    flag := 1;

                                    exit;

                        end if;

            end loop;

            if mod(no,2)=0) then

                        dbms_output.put_line('Number is even ' || no);

            else

                        dbms_output.put_line('Number is odd ' || no);

            end if;

           if (flag = 1) then

                        dbms_output.put_line('Number is Not prime ' );

            else

                        dbms_output.put_line('Number is prime ' );

            end if;

end;

 Output

Enter value for no: 5                                     Enter value for no: 1

Number is odd                                              Number is odd

Number is Not prime                                    Number is Not prime

 Write a program to print the Fibonacci series.

 declare

            no number;

            f1 number;

            f2 number;

            f3 number;

begin

            no:=&no;

            f1 := 0;

            f2 := 1;

            dbms_output.put_line(' Fibonnacci series is');

            dbms_output.put_line(f1);

            dbms_output.put_line(f2);

            for i in 3..no

            loop

                        f3 := f1 + f2;

                        dbms_output.put_line(f3);

                        f1 := f2;

                        f2 := f3;

            end loop;

end;

Output:

Enter value for no: 7

Fibonnacci series is

0

1

1

2

3

5

8

Write a program to find the factorial of any number given by user.

 declare

            i number:=1;

            no number:=&no;

            fact number :=1;

begin

            if (no<=0) then

                        dbms_output.put_line('enter positive number');

            else

                        fact := 1;

                        while (i <= no)

                        loop

                                    fact := fact*i;

                                    dbms_output.put_line('fact is : ' || fact);

                                    i := i+1;

                        end loop;

                dbms_output.put_line('factorial is : ' || fact);

            end if;

end;

 OUTPUT

SQL>/

Enter value for no: 3

old   3:    no number:=&no;

new   3:    no number:=3;

fact is : 1

fact is : 2

fact is : 6

factorial is : 6

 Write a program to find the reverse of any number given by user.

 declare

            no number;

            right_digit number;

begin

            no:=&no;

            while (no>=1)

            loop

                        right_digit:=right_digit||trunc(mod(no,10),0);

                        no:=no/10;

            end loop;

            dbms_output.put_line(right_digit);

end;

/

OUTPUT

SQL> /

Enter value for no: 123456789

987654321

Write a program to find the number is palindrome or not.

 declare

no number;

            right_digit number;

orig_no number;

begin

no:=&no;

orig_no := no;

dbms_output.put_line(orig_no);

while (no>=1)

loop

right_digit:=right_digit||trunc(mod(no,10),0);

                        no:=no/10;

            end loop;

dbms_output.put_line(‘Reverse number is : ‘ || right_digit);

if (orig_no=right_digit) then

                        dbms_output.put_line('It is palindrome');

else

                        dbms_output.put_line('It is not palindrome');

end if;

end;

 Output:

Enter value for no: 121                                           Enter value for no: 12345

Reverse number is : 121                                        Reverse number is : 54321

It is palindrome                                                       It is not palindrome

 Write a program to find the given string is palindrome or not.

 declare

            given_string varchar2(20):='&given_string';

            str_len number(3);

            inv_str varchar2(20);

            orig_str varchar(20);

begin

            orig_str := given_string;

            str_len := length(given_string);

            for i in reverse 1..str_len

            loop

                        inv_str := inv_str || substr(given_string,i,1);

            end loop;

            dbms_output.put_line('The given string was : ' || given_string);

            dbms_output.put_line('The inverted string is : ' || inv_str);

           

            if (orig_str = inv_str) then

                        dbms_output.put_line('The given string is Palindrome');

            else

                        dbms_output.put_line('The given string is Not Palindrome');

            end if;

end;

 Output:

Enter value for given_string: madam

The given string was : madam

The inverted string is : madam

The given string is Palindrome

   

Write a program to check wether student has got First, second class or distinction.

 

declare

per number;

begin

            per:= &per;

            if (per >= 70) then

                        dbms_output.put_line('Percentage is ' || per || ' You have got distinction');

            elsif (per<=69 AND per >=60) then

                        dbms_output.put_line('Percentage is ' || per || ' You have got First Class');

            elsif (per <60 And per >35) then

                        dbms_output.put_line('Percentage is ' || per || ' You have got second class');

            else

                        dbms_output.put_line('Percentage is ' || per || ' You have FAILED');

            end if;

end;

 Output:

SQL > Enter the value of per : 89

SQL > Percentage is 89 You have got distinction.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.