PL/SQL Program to Print Patterns
Here you will know PL/SQL program to print patterns of star, numbers and alphabets by using for and while loops.
Pattern 1
1 2 3 4 5 6 7 8 9 10 11 12 13 | DECLARE rows NUMBER := 6; -- to print number of rows i NUMBER; j NUMBER; BEGIN FOR i IN 1..rows LOOP FOR j IN 1..i LOOP DBMS_OUTPUT.PUT('*'); -- Print asterisk END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | DECLARE rows NUMBER := 5; -- Number of rows i NUMBER; -- counter for rows j NUMBER; -- counter for columns BEGIN FOR i IN 1..rows LOOP -- Print spaces for the left angle FOR j IN 1..(rows - i) LOOP DBMS_OUTPUT.PUT(' '); -- Print spaces END LOOP; -- Print asterisks FOR j IN 1..i LOOP DBMS_OUTPUT.PUT('*'); -- Print asterisks END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | DECLARE rows NUMBER := 5; -- Number of rows i NUMBER; -- counter for rows j NUMBER; -- counter for spaces and star BEGIN FOR i IN 0..(rows - 1) LOOP FOR j IN 1..i LOOP DBMS_OUTPUT.PUT(' '); -- Print spaces END LOOP; -- Print asterisks FOR j IN 1..(rows - i) LOOP DBMS_OUTPUT.PUT('*'); -- Print star END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | DECLARE rows NUMBER := 5; -- Number of rows i NUMBER; -- counter for rows j NUMBER; -- counter for asterisks BEGIN FOR i IN 1..rows LOOP -- Print asterisks FOR j IN 1..(rows - i + 1) LOOP DBMS_OUTPUT.PUT('*'); -- Print star END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | DECLARE rows NUMBER := 5; -- Number of rows for the upper half i NUMBER; -- counter for rows j NUMBER; -- counter for spaces and star BEGIN -- Upper half part FOR i IN 1..rows LOOP -- Print leading spaces FOR j IN 1..(rows - i) LOOP DBMS_OUTPUT.PUT(' '); -- Print spaces END LOOP; -- Print star FOR j IN 1..(2 * i - 1) LOOP DBMS_OUTPUT.PUT('*'); END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; -- Lower half part FOR i IN (rows - 1)..1 BY -1 LOOP -- Print leading spaces FOR j IN 1..(rows - i) LOOP DBMS_OUTPUT.PUT(' '); -- Print spaces END LOOP; -- Print star FOR j IN 1..(2 * i - 1) LOOP DBMS_OUTPUT.PUT('*'); END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 6
1 2 3 4 5 6 7 8 9 10 11 12 13 | DECLARE size NUMBER := 5; -- Size of the square pattern i NUMBER; -- counter for rows j NUMBER; -- counter for columns BEGIN FOR i IN 1..size LOOP FOR j IN 1..size LOOP DBMS_OUTPUT.PUT('*'); -- Print asterisk END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | DECLARE size NUMBER := 5; -- square size i NUMBER; -- counter for rows j NUMBER; -- counter for columns BEGIN FOR i IN 1..size LOOP FOR j IN 1..size LOOP IF i = 1 OR i = size OR j = 1 OR j = size THEN DBMS_OUTPUT.PUT('*'); -- Print star ELSE DBMS_OUTPUT.PUT(' '); -- Print space for the hollow part END IF; END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | DECLARE rows NUMBER := 5; -- Number of rows in the upper half i NUMBER; -- counter for rows j NUMBER; -- counter for columns BEGIN -- Upper half FOR i IN 1..rows LOOP -- Print spaces for alignment FOR j IN 1..(rows - i) LOOP DBMS_OUTPUT.PUT(' '); -- Print spaces END LOOP; -- Print star FOR j IN 1..i LOOP DBMS_OUTPUT.PUT('*'); -- Print star END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; -- Lower half FOR i IN (rows - 1)..1 BY -1 LOOP -- Print spaces for alignment FOR j IN 1..(rows - i) LOOP DBMS_OUTPUT.PUT(' '); -- Print spaces END LOOP; -- Print star FOR j IN 1..i LOOP DBMS_OUTPUT.PUT('*'); -- Print star END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Pattern 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | DECLARE rows NUMBER := 5; -- Number of rows i NUMBER; -- counter for rows j NUMBER; -- counter for star BEGIN -- Upper half FOR i IN 1..rows LOOP FOR j IN 1..i LOOP DBMS_OUTPUT.PUT('*'); -- Print star END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; -- Lower half of the diamond FOR i IN (rows - 1)..1 BY -1 LOOP FOR j IN 1..i LOOP DBMS_OUTPUT.PUT('*'); -- Print star END LOOP; DBMS_OUTPUT.PUT_LINE(''); -- Move to the next line END LOOP; END; / |
Check out our other PL/SQL programs examples