Monday, January 27, 2014

Pattern Printing in PLSql

1:-

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


DECLARE
  STAR VARCHAR2(1):='*';
  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:-


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

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;


13:-

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

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;


14:-

    *   
   *#*  
  *#**# 
 *#*#*#*
*#*#**#*#

DECLARE
 STAR VARCHAR2(1):= '*';
 SPACE VARCHAR2(1):= ' ';
 P_PRINT VARCHAR2(100);
 S_STAR VARCHAR2(100);
 P_STAR VARCHAR2(100);
 HASH VARCHAR2(1):= '#';
 BEGIN
    FOR I IN 1..&&length
    LOOP
        IF MOD(I,2) !=0
        THEN
            P_STAR:= S_STAR;
            S_STAR:= S_STAR||STAR;   
            P_PRINT:= RPAD(LPAD(S_STAR,&&length,SPACE)||P_STAR,(2*&&length)-1,SPACE);
            DBMS_OUTPUT.PUT_LINE(P_PRINT);
        ELSE
            P_STAR:= S_STAR;
            S_STAR:= S_STAR||HASH;   
            P_PRINT:= RPAD(LPAD(S_STAR,&&length,SPACE)||P_STAR,(2*&&length)-1,SPACE);
            DBMS_OUTPUT.PUT_LINE(P_PRINT);
         END IF;      
    END LOOP;
END;


15:-

#####
&&&&
###
&&
#

DECLARE
 SYMBEL_1 VARCHAR2(50):= '&';
 SYMBEL_2 VARCHAR2(50):= '#';
 STAR VARCHAR2(50);
 HASH VARCHAR2(50);
 S_STAR VARCHAR2(50);
 S_HASH VARCHAR2(50);
BEGIN
    FOR I IN 1..&&A
    LOOP
        STAR:= STAR||SYMBEL_1;
        HASH:= HASH||SYMBEL_2;
    END LOOP;
    FOR I IN 1..&&A
    LOOP
        IF MOD(I,2)=0
        THEN
            S_STAR:= SUBSTR(STAR,I,&&A);
            DBMS_OUTPUT.PUT_LINE(S_STAR);
        ELSE
            S_HASH:= SUBSTR(HASH,I,&A);
            DBMS_OUTPUT.PUT_LINE(S_HASH);
        END IF;
    END LOOP;
END;



16:-

#
&&
###
&&&&
#####

DECLARE
 SYMBEL_1 VARCHAR2(50):= '&';
 SYMBEL_2 VARCHAR2(50):= '#';
 STAR VARCHAR2(50);
 HASH VARCHAR2(50);
 S_STAR VARCHAR2(50);
 S_HASH VARCHAR2(50);
BEGIN
    FOR I IN 1..&&A
    LOOP
        STAR:= STAR||SYMBEL_1;
        HASH:= HASH||SYMBEL_2;
    END LOOP;
    FOR I IN 1..&A
    LOOP
        IF MOD(I,2)=0
        THEN
            S_STAR:= SUBSTR(STAR,1,I);
            DBMS_OUTPUT.PUT_LINE(S_STAR);
        ELSE
            S_HASH:= SUBSTR(HASH,1,I);
            DBMS_OUTPUT.PUT_LINE(S_HASH);
        END IF;
    END LOOP;
END;



17:-

*#*#*
#*#*
*#*
#*
*



DECLARE
 STAR VARCHAR2(50);
 S_STAR VARCHAR2(50);
 S_HASH VARCHAR2(50);
BEGIN
    FOR I IN 1..&&A
    LOOP
        IF MOD(I,2)!=0
        THEN
            STAR:= STAR||'*';
        ELSE
            STAR:= STAR||'#';
        END IF;
    END LOOP;
    FOR I IN 1..&&A
    LOOP
        IF MOD(I,2)!=0
        THEN
            S_STAR:= SUBSTR(STAR,I,&&A);
            DBMS_OUTPUT.PUT_LINE(S_STAR);
        ELSE
            S_HASH:= SUBSTR(STAR,I,&A);
            DBMS_OUTPUT.PUT_LINE(S_HASH);
        END IF;
    END LOOP;
END;


18:-

*
#*
*#*
#*#*
*#*#*


DECLARE
 STAR VARCHAR2(50);
 HASH VARCHAR2(50);
 S_STAR VARCHAR2(50);
 S_HASH VARCHAR2(50);
BEGIN
    FOR I IN 1..&&A
    LOOP
        IF MOD(I,2)!=0
        THEN
            STAR:= STAR||'*';
            HASH:= HASH||'#';
        ELSE
            STAR:= STAR||'#';
            HASH:= HASH||'*';
        END IF;
        END LOOP;
    FOR I IN 1..&A
    LOOP
        IF MOD(I,2)!=0
        THEN
            S_STAR:= SUBSTR(STAR,1,I);
            DBMS_OUTPUT.PUT_LINE(S_STAR);
        ELSE
            S_HASH:= SUBSTR(HASH,1,I);
        DBMS_OUTPUT.PUT_LINE(S_HASH);
        END IF;
    END LOOP;   
END;



19:-


      *
    *   *   
   *     *  
  *       * 
 *         *
*           *


DECLARE
 STAR VARCHAR2(1):= '*';
 SPACE VARCHAR2(1):= ' ';
 S_STAR VARCHAR2(13);
BEGIN
    DBMS_OUTPUT.PUT_LINE('      *');
    FOR I IN 1..5
    LOOP
        S_STAR:=RPAD(RPAD(LPAD('*',6-I,' '),7+I,' ')||STAR,13,SPACE);
        DBMS_OUTPUT.PUT_LINE(S_STAR);
    END LOOP;
END;


20:-

*           *
 *         *
  *       * 
   *     *  
    *   *   
      *
     
     
DECLARE
 STAR VARCHAR2(1):= '*';
 SPACE VARCHAR2(1):= ' ';
 S_STAR VARCHAR2(13);
BEGIN
    FOR I IN REVERSE 1..5
    LOOP
        S_STAR:=RPAD(RPAD(LPAD('*',6-I,' '),7+I,' ')||STAR,13,SPACE);
        DBMS_OUTPUT.PUT_LINE(S_STAR);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('      *');
END;


21:-

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


DECLARE
 STAR VARCHAR2(1):= '*';
 SPACE VARCHAR2(1):= ' ';
 S_STAR VARCHAR2(13);
BEGIN
    DBMS_OUTPUT.PUT_LINE('      *');
    FOR I IN 1..5
    LOOP
        S_STAR:=RPAD(RPAD(LPAD('*',6-I,' '),7+I,' ')||STAR,13,SPACE);
        DBMS_OUTPUT.PUT_LINE(S_STAR);
    END LOOP;
    FOR I IN REVERSE 1..5
    LOOP
        S_STAR:=RPAD(RPAD(LPAD('*',6-I,' '),7+I,' ')||STAR,13,SPACE);
        DBMS_OUTPUT.PUT_LINE(S_STAR);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('      *');
END;    



23:-


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

DECLARE
 STAR VARCHAR2(1):= '*';
 SPACE VARCHAR2(1):= ' ';
 S_STAR VARCHAR2(13);
 P_STAR VARCHAR2(14);
BEGIN
    DBMS_OUTPUT.PUT_LINE('      *');
    FOR I IN 1..5
    LOOP
        S_STAR:=RPAD(RPAD(LPAD('*',6-I,' '),7+I,' ')||STAR,13,SPACE);
        DBMS_OUTPUT.PUT_LINE(S_STAR);
    END LOOP;
    FOR I IN 1..7
    LOOP
        P_STAR:= P_STAR||STAR||SPACE;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE(P_STAR);
END;


24:-

* * * * * * *
*           *
 *         *
  *       * 
   *     *  
    *   *   
      *
     
DECLARE
 STAR VARCHAR2(1):= '*';
 SPACE VARCHAR2(1):= ' ';
 S_STAR VARCHAR2(13);
 P_STAR VARCHAR2(14);
BEGIN
FOR I IN 1..7
LOOP
P_STAR:= P_STAR||STAR||SPACE;
END LOOP;
DBMS_OUTPUT.PUT_LINE(P_STAR);
    FOR I IN REVERSE 1..5
    LOOP
        S_STAR:=RPAD(RPAD(LPAD('*',6-I,' '),7+I,' ')||STAR,13,SPACE);
        DBMS_OUTPUT.PUT_LINE(S_STAR);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('      *');
END;
 

No comments:

Post a Comment