PL/SQL Program to Print Patterns

Here you will get plsql programs to print patterns of stars, numbers and alphabets.

Pattern 1:

*
**
***
****
*****
declare
	n number:=5;
	i number;
	j number;
begin
	for i in 1..n
	loop
		for j in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
	end loop;
end;
/

Pattern 2:

*****
****
***
**
*
declare
	n number:=5;
	i number;
	j number;
begin
	for i in reverse 1..n
	loop
		for j in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
	end loop;
end;
/

Pattern 3:

    *
   **
  ***
 ****
*****
declare
	n number:=5;
	i number;
	j number;
	k number;
begin
	for i in 1..n
	loop
		for j in 1..n-i
		loop
			dbms_output.put(' ');
		end loop;

		for k in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
	end loop;
end;
/

Pattern 4:

*****
 ****
  ***
   **
    *
declare
	n number:=5;
	i number;
	j number;
	k number;
begin
	for i in reverse 1..n
	loop
		for j in 1..n-i
		loop
			dbms_output.put(' ');
		end loop;

		for k in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
	end loop;
end;
/

Pattern 5:

    *
   ***
  *****
 *******
*********
declare
	n number:=10;
	i number:=1;
	j number;
	k number;
begin
	while i<n
	loop
		j:=1;
		while j<n-i
		loop
			dbms_output.put(' ');
			j:=j+2;
		end loop;

		for k in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
		i:=i+2;
	end loop;
end;
/

Pattern 6:

*********
 *******
  *****
   ***
    *
declare
	i number:=9;
	j number;
	k number;
begin
	while i>=1
	loop
		j:=9;
		while j>i
		loop
			dbms_output.put(' ');
			j:=j-2;
		end loop;

		for k in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
		i:=i-2;
	end loop;
end;
/

Pattern 7:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
declare
	n number:=10;
	i number:=1;
	j number;
	k number;
begin
	while i<n
	loop
		j:=1;
		while j<n-i
		loop
			dbms_output.put(' ');
			j:=j+2;
		end loop;

		for k in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
		i:=i+2;
	end loop;

	i:=7;
	
	while i>=1
	loop
		j:=9;
		while j>i
		loop
			dbms_output.put(' ');
			j:=j-2;
		end loop;

		for k in 1..i
		loop
			dbms_output.put('*');
		end loop;
		
		dbms_output.new_line;
		i:=i-2;
	end loop;	
end;
/

Comment below if you need any other pattern. I will try to add it here.

49 thoughts on “PL/SQL Program to Print Patterns”

    1. declare
      n number:=5;
      i number;
      j number;
      begin
      for i in 1..n
      loop
      for j in 1..i
      loop
      dbms_output.put(‘1’);
      end loop;

      dbms_output.new_line;
      end loop;
      end;
      /

        1. declare
          n number:=5;
          i number;
          j number;
          begin
          for i in 1..n
          loop
          for j in 1..i
          loop
          dbms_output.put(‘J’);
          end loop;
          dbms_output.new_line;
          end loop;
          end;
          /

    2. Declare
      n number(1);=5:
      i nimber(1):
      j number (1):
      Begin
      For i in 1..n
      loop
      for j in j..i
      loop
      dbms_output.put(j):
      end loop:
      dbms_output.new_line:
      end loop:
      End:
      /

    3. declare
      n number:=5;
      i number;
      j NUMBER := 1;
      begin
      FOR k IN 1..n LOOP
      i:=i||j;
      j:=j+1;
      dbms_output.put_line(i);
      END LOOP;
      END;

    4. declare
      i number;

      k number;
      n number:=4;
      begin
      for i in 1..n
      loop

      k:=1;
      while k<i+1
      loop
      dbms_output.put(k);
      k:=k+1;
      end loop;

      dbms_output.new_line;
      end loop;
      end;

    5. declare
      n number:=5;
      i number;
      j number;

      begin
      for i in 1..n loop
      for j in 1..i loop
      dbms_output.put(j);
      end loop;
      dbms_output.new_line;
      end loop;
      end;
      /

    6. set serveroutput on
      declare
      n number:=5;
      i number;
      j number;
      k PLS_integer:=1;
      begin
      for i in 1..n
      loop
      k:=1;
      for j in 1..i
      loop
      dbms_output.put(k);
      k := k+1;
      end loop;

      dbms_output.new_line;
      end loop;
      end;
      /

    7. DECLARE
      I_NUM NUMBER:=1234;
      PATTERN NUMBER;
      BEGIN
      FOR I IN 1..LENGTH(I_NUM) LOOP
      PATTERN:=SUBSTR(I_NUM,1,I);
      DBMS_OUTPUT.PUT_LINE(PATTERN);
      END LOOP;
      END;

      OUTPUT LIKE:
      1
      12
      123
      1234

    1. declare
      n number:=3;
      i number ;
      k number :=0;
      j number ;
      begin
      for i in 1..n
      loop
      for j in 1..n
      loop
      dbms_output.put(j+k);
      end loop;
      dbms_output.new_line;
      k:=k+3;
      end loop;
      end;

    2. set serveroutput on
      declare
      n number:=3;
      i number;
      j number;
      k pls_integer:=1;
      begin
      for i in 1..n
      loop
      for j in 1..n
      loop
      dbms_output.put(k);
      k:=k+1;
      end loop;

      dbms_output.new_line;
      end loop;
      end;
      /

    3. Begin
      for i in 1..9
      loop
      DBMS_OUTPUT.PUT(i);
      IF MOD(i,3)=0 THEN
      DBMS_OUTPUT.NEW_LINE;
      CONTINUE;
      END IF;
      End loop;
      END;

  1. pl/sql is not able to add space
    declare
    n number:=10;
    i number:=1;
    j number;
    k number;
    begin
    while i<n
    loop
    j:=1;
    while j=1
    loop
    j:=9;
    while j>i
    loop
    dbms_output.put(‘ ‘);
    j:=j-2;
    end loop;

    for k in 1..i
    loop
    dbms_output.put(‘*’);
    end loop;

    dbms_output.new_line;
    i:=i-2;
    end loop;
    end;
    /

  2. IF AM GIVING INPUT AS 1 THEN I NEED TO GET OUTPUT AS 1,IF INPUT IS 2 THEN OUTPUT SHOULD BE AS 2
    2 2
    IF INPUT IS 3 THEN OUTPUT SHOULD BE 3
    3 3
    3 3 3

    1. Amit Kumar BISWAS

      DECLARE
      n NUMBER:=5;
      i NUMBER;
      j NUMBER;
      BEGIN
      FOR i IN 1..n LOOP
      FOR j IN 1..i LOOP
      DBMS_OUTPUT.PUT(i);
      END LOOP;
      DBMS_OUTPUT. NEW_LINE;
      END LOOP;
      END;
      /

    1. set serveroutput on
      declare
      n number:=3;
      i number;
      j number;
      k pls_integer:=1;
      begin
      for i in 1..n
      loop
      for j in 1..i
      loop
      dbms_output.put(k);
      k:=k+1;
      end loop;

      dbms_output.new_line;
      end loop;
      end;
      /

    1. DECLARE
      I NUMBER;
      J NUMBER;
      BEGIN
      FOR I IN 1..5
      LOOP
      FOR J IN 1..I
      LOOP
      DBMS_OUTPUT.PUT(‘* ‘);
      END LOOP;
      DBMS_OUTPUT.NEW_LINE;
      END LOOP;
      END;

  3. Hello!
    How to Write a PL/SQL program to print the following series using nested for loop.
    0 0 0 0 0
    1 2 3 4 5
    2 4 6 8 10
    3 6 9 12 15
    4 8 12 16 20
    5 10 15 20 25

    1. Mohammed Almansour

      Solve Pattern program is

      Declare
      K number;
      I number;
      J number;
      Begin
      For i in 0..5 loop
      K := i ;
      For j in 0..5 loop
      Dbms_output.put_line ( K ) ;
      K:=K+i ;
      End loop;
      Dbms_output.new_line;
      End loop;
      End;

    2. /* 1 2 3 4 5
      2 4 6 8 10
      3 6 9 12 15
      4 8 12 16 20
      5 10 15 20 25 */

      DECLARE
      V VARCHAR2(20);
      K VARCHAR2(10);
      L VARCHAR2(10):=0;
      BEGIN
      FOR I IN 1..5 LOOP
      K:=I;
      FOR J IN 1..5 LOOP
      L:=K*J;
      V:=V||’ ‘||L;
      END LOOP;
      DBMS_OUTPUT.PUT_LINE(V);
      V:=NULL;
      END LOOP;
      END;
      /

  4. Hi, great work!! thanks for all this info here. Could you also tell us how to print just the diagonals of a square?

    1. declare
      i number:=1;
      j number:=0;
      m number:=1;
      begin
      while(j<4)
      loop
      for k in i..(i+j)
      loop
      dbms_output.put(k);
      end loop;
      dbms_output.new_line;
      j:=j+1;
      i:=i+m;
      m:=m+1;
      end loop;
      END;
      /

    1. declare
      var_name varchar2(10):=’QUERY’;
      begin
      for i in 1.. length(var_name)
      loop
      dbms_output.put_line(substr(var_name,1,i));
      end loop;
      end;

  5. Given string loop and height 10, the magical pattern is
    l
    loo
    loopl
    looploo
    looploopl
    looplooploo
    looplooploopl
    looplooplooploo
    looplooplooploopl
    looplooplooplooploo

    1. DECLARE
      n NUMBER:=&n;
      i NUMBER;
      j NUMBER;
      k NUMBER:=1;
      BEGIN
      FOR i IN 0..n-1
      LOOP
      FOR j IN 0..i
      LOOP
      if MOD(k,2) 0 THEN
      dbms_output.put(MOD(k,2));
      else
      dbms_output.put(2);

      end if;
      k:=k+1;
      END LOOP;
      dbms_output.new_line();
      END LOOP;

      END;
      /

  6. Can anybody please help with the pl sql query for fetching and printing data in a row in below pattern?

    1~ABC~20-Jun-1998^2~CDE~09-Aug-1987^……

    The above data already stored in a table, need here only is to print the fetched data in above pattern

Leave a Comment

Your email address will not be published. Required fields are marked *