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.
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
Input
Output:
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 .
Rectangle Pattern with PL/SQL
To form a rectangular pattern inner loop is iterated for different number of times than the outer loop. Inner loop indicate the number of columns and the outer loop as number of rows in the rectangle.
Rectangle with length as 5 and breadth as 3
*****
*****
*****
C
DECLARE
--declare variables
numb_row NUMBER;
numb_col NUMBER;
BEGIN
--To get input from user
numb_row := &rows;
numb_col := &cols;
FOR i IN 1..numb_row LOOP
FOR j IN 1..numb_col LOOP
DBMS_OUTPUT.PUT( '* ' );
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END LOOP;
END;
|
Input:
rows:3
columns:5
Rectangle Pattern Output
Rectangle Pattern Output
Conclusion:
Pattern are generated using nested loops .A pattern of any shape can be generated by specifying the number of rows and column along with symmetry . It enhances coding proficiency and enables creative design.
Â
Last Updated :
06 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...