Square Pattern using PL/SQL
To print a square pattern inner loop is iterated for the same number of times as the outer loop.
Square of Length 3
***
***
***
C
SET SERVEROUTPUT ON; DECLARE -- declare varable N NUMBER; BEGIN --user defined input N := &I; FOR I IN 1..N LOOP FOR J IN 1..N LOOP DBMS_OUTPUT.PUT( '*' ); END LOOP; DBMS_OUTPUT.NEW_LINE; END LOOP; END; |
Input
Output:
Explanation:
For the above example, the inner loop is iterated over the outer for the same number of times. Input for the instance is 5 hence the square pattern is displayed with 5 rows and columns .
Print Patterns in PL/SQL
You have given a number n then you have to print the number in a right-angled pyramid of *
Examples:
Input : 3
Output :
*
**
***
Input : 7
Output :
*
**
***
****
*****
******
*******
C
DECLARE -- declare variable n, --I AND J of datatype number N NUMBER := 7; I NUMBER; J NUMBER; BEGIN -- loop from 1 to n FOR I IN 1..N LOOP FOR J IN 1..I LOOP DBMS_OUTPUT.PUT( '*' ) ; -- printing * END LOOP; DBMS_OUTPUT.NEW_LINE; -- for new line END LOOP; END; --Program End |
Output:
*
**
***
****
*****
******
*******
A pattern of any shape can be generated by specifying the number of rows and column along with symmetry and regularity. To form a 2D shape one variable is iterated over the other to form the desired pattern.