Open In App

Print Patterns in PL/SQL

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

Input

Output:

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

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.


 



Similar Reads

Program to print hollow rectangle or square star patterns
Hollow rectangle star pattern : The task is print below hollow pattern of given dimension. ******************** * * * * * * * * ******************** Explanation: Input number of rows and columns.For rows of rectangle run the outer loop from 1 to rows.for (i = 1; i < = rows; i++)For column of rectangle run the inner loop from 1 to columns. for (j
4 min read
Program to print right and left arrow patterns
Write to program to print the right arrow pattern and the left arrow pattern formed of stars. Input is an odd number n which represents height and width of pattern to be printed.Examples: Input : n = 9 Output : * * * * ********* * * * * * * * * ********* * * * * Input : n = 7 Output : * * * ******* * * * * * * ******* * * * C/C++ Code // C++ progra
8 min read
Print different star patterns in SQL
Let's see how we can print the pattern of various type using SQL. Syntax : Declare @variable_name DATATYPE -- first declare all the -- variables with datatype -- like (int) select @variable = WITH_ANY_VALUE -- select the variable and -- initialize with value while CONDITION -- condition like @variable > 0 begin -- begin print replicate('*', @var
2 min read
Program to print diagonal star patterns
Time Complexity: O(N) where N is number of nodes in a given binary tree Auxiliary Space: O(N)For the given input, this program prints the following pattern. The input must be an odd number.Examples: Input : 7 Output : ******* ** ** * * * * * * * * * * * ** ** ******* Below is the code printing above pattern : C/C++ Code // CPP program to print diag
10 min read
Recursive program to print triangular patterns
We have discussed iterative pattern printing in previous post. Examples: Input : 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * Algorithm:- step 1:- first think for the base condition i.e. number less than 0 step 2:-do the recursive calls till number less than 0 i.e:- printPartten(n-1, k+1); step 3:-print the spaces step 4:-then
8 min read
Programs to print Triangle and Diamond patterns using recursion
This article is aimed at giving a recursive implementation for pattern printing. Pattern 1: Example:Input: 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Implementation: C/C++ Code // Program to print the given pattern #include <iostream> using namespace std; void print_asterisk(int asterisk) { if (asterisk == 0) return;
21 min read
Programs to print Interesting Patterns
Program to print the following pattern: Examples : Input : 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program is divided into four parts. C/C++ Code // C++ program to print // the given pattern #include<iostream> using namespace std; void pattern(int n
22 min read
Program to print solid and hollow square patterns
For any given number n, print Hollow and solid Squares and Rhombus made with stars(*). Examples: Input : n = 4 Output : Solid Square: **** **** **** **** Hollow Square: **** * * * * **** 1. Solid Square : Solid Square is easiest among all given patterns. To print Solid square with n rows, we should use two loops iterating n times both. Where the ou
6 min read
Program to print solid and hollow rhombus patterns
For any given number n, print Hollow and solid Squares and Rhombus made with stars(*). Examples: Input : n = 4 Output : Solid Rhombus: **** **** **** **** Hollow Rhombus: **** * * * * **** 1. Solid Rhombus : Making Solid Rhombus is a bit similar to making solid square rather than the concept that for each ith row we have n-i blank spaces before sta
12 min read
C++ Program To Print Pyramid Patterns
In this article, we will discuss the following top pattern programs in C++ using star ( * ) , numbers or other characters. [caption width="800"]Pyramid Patterns in C++[/caption]Simple Pyramid PatternFlipped Simple Pyramid PatternInverted Pyramid PatternFlipped Inverted Pyramid PatternTriangle PatternInverted Triangle PatternHalf Diamond PatternFlip
24 min read
Practice Tags :