Pattern Printing in Oracle SQL

1 ————

*
**
***
****
*****

DECLARE
P_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR := P_STAR||’*’;
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
END;

OR

DECLARE
STAR VARCHAR2(5):=’*’;
P_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR := LPAD(STAR,I,STAR);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
END;

2 ————————————–

&
&*
&*&
&*&*
&*&*&

DECLARE
P_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
IF MOD(I,2)=0
THEN
P_STAR := P_STAR||’*’;
DBMS_OUTPUT.PUT_LINE(P_STAR);
ELSE
P_STAR:=   P_STAR||’&’;
DBMS_OUTPUT.PUT_LINE(P_STAR);
END IF;
END LOOP;
END;

3 —————————————

****
****
***
**
*

DECLARE
P_STAR VARCHAR2(5):= ‘*****’;
S_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
S_STAR := SUBSTR(P_STAR,I);
DBMS_OUTPUT.PUT_LINE(S_STAR);
END LOOP;
END;

4 ———————————

*
**
***
****
*****
****
***
**
*

DECLARE
V_STAR VARCHAR2(1):= ‘*’;
P_STAR VARCHAR2(5);
S_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR:= LPAD(V_STAR,I,V_STAR);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
FOR I IN 1..5
LOOP
S_STAR := SUBSTR(P_STAR,I+1);
DBMS_OUTPUT.PUT_LINE(S_STAR);
END LOOP;
END;

5 —————————————————-

*****
****
***
**
*
*
**
***
****
*****

DECLARE
V_STAR VARCHAR2(5):= ‘*****’;
P_STAR VARCHAR2(5);
S_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR := SUBSTR(V_STAR,I);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
FOR I IN 1..5
LOOP
S_STAR := LPAD(P_STAR,I,P_STAR);
DBMS_OUTPUT.PUT_LINE(S_STAR);
END LOOP;
END;

6 ——————————————

*****
****
***
**
*
**
***
****
*****

DECLARE
V_STAR VARCHAR2(5):= ‘*****’;
P_STAR VARCHAR2(5);
S_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR := SUBSTR(V_STAR,I);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
FOR I IN 1..4
LOOP
P_STAR := P_STAR||’*’;
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
END;

7 ——————————

*****
****
***
**
*

DECLARE
SPACE VARCHAR2(1):= ‘ ‘;
V_STAR VARCHAR2(5):= ‘*****’;
P_STAR VARCHAR(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR:= LPAD(SUBSTR(V_STAR,I),5,SPACE);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
END;

8 ——————————
*
**
***
****
*****

DECLARE
STAR VARCHAR2(1):= ‘*’;
SPACE VARCHAR2(1):= ‘ ‘;
P_NO VARCHAR2(5);
P_STAR VARCHAR2(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR:= P_STAR||STAR;
FOR J IN 1..5
LOOP
P_NO:= LPAD(P_STAR,5,SPACE);
END LOOP;
DBMS_OUTPUT.PUT_LINE(P_NO);
END LOOP;
END;

9 ——————————
*****
****
***
**
*
*
**
***
****
*****

DECLARE
SPACE VARCHAR2(1):= ‘ ‘;
V_STAR VARCHAR2(5):= ‘*****’;
P_STAR VARCHAR(5);
S_STAR VARCHAR(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR:= LPAD(SUBSTR(V_STAR,I),5,SPACE);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
FOR J IN 1..5
LOOP
S_STAR:= RPAD(SUBSTR(P_STAR,J),5,’*’);
DBMS_OUTPUT.PUT_LINE(S_STAR);
END LOOP;
END;

10 ——————————

*****
****
***
**
*
**
***
****
*****

DECLARE
SPACE VARCHAR2(1):= ‘ ‘;
V_STAR VARCHAR2(5):= ‘*****’;
P_STAR VARCHAR(5);
S_STAR VARCHAR(5);
BEGIN
FOR I IN 1..5
LOOP
P_STAR:= LPAD(SUBSTR(V_STAR,I),5,SPACE);
DBMS_OUTPUT.PUT_LINE(P_STAR);
END LOOP;
FOR J IN 1..5
LOOP
S_STAR:= RPAD(SUBSTR(P_STAR,J+1),5,’*’);
DBMS_OUTPUT.PUT_LINE(S_STAR);
END LOOP;
END;

11 ——————————

**** ****
***   ***
**     **
*       *

DECLARE
STAR VARCHAR2(1):= ‘*’;
SPACE VARCHAR2(1):= ‘ ‘;
P_PRINT VARCHAR2(9);
S_SPACE VARCHAR2(9);
P_SPACE VARCHAR2(9);
BEGIN
FOR I IN 1..4
LOOP
P_SPACE:= S_SPACE;
S_SPACE:= S_SPACE||SPACE;
P_PRINT:= RPAD(LPAD(S_SPACE,5,STAR)||P_SPACE,9,STAR);
DBMS_OUTPUT.PUT_LINE(P_PRINT);
END LOOP;
END;

12 ——————————

*        *
**      **
***    ***
****  ****
**********

13 ——————————

****  ****
***    ***
**      **
*        *
**      **
***    ***
****  ****

DECLARE
STAR VARCHAR2(1):= ‘*’;
SPACE VARCHAR2(1):= ‘ ‘;
P_PRINT VARCHAR2(9);
S_SPACE VARCHAR2(9);
P_SPACE VARCHAR2(9);
BEGIN
FOR I IN 1..4
LOOP
P_SPACE:= S_SPACE;
S_SPACE:= S_SPACE||SPACE;
P_PRINT:= RPAD(LPAD(S_SPACE,5,STAR)||P_SPACE,9,STAR);
DBMS_OUTPUT.PUT_LINE(P_PRINT);
END LOOP;
FOR I IN 1..3
LOOP
S_SPACE:= P_SPACE;
P_SPACE:= SUBSTR(P_SPACE,1,4-(I+1));
P_PRINT:= RPAD(LPAD(S_SPACE,5,STAR)||P_SPACE,9,STAR);
DBMS_OUTPUT.PUT_LINE(P_PRINT);
END LOOP;
END;

14 ———————————-

*
***
*****
*******
*********

DECLARE
STAR VARCHAR2(1):= ‘*’;
SPACE VARCHAR2(1):= ‘ ‘;
P_PRINT VARCHAR2(9);
S_STAR VARCHAR2(9);
P_STAR VARCHAR2(9);
BEGIN
FOR I IN 1..5
LOOP
P_STAR:= S_STAR;
S_STAR:= S_STAR||STAR;
P_PRINT:= RPAD(LPAD(S_STAR,5,SPACE)||P_STAR,9,SPACE);
DBMS_OUTPUT.PUT_LINE(P_PRINT);
END LOOP;
END;

 

 

Advertisements