Patterns: ========= Syntax: outerloop_for_row{ innerloop_for_column{ } } pattern ==> divided into rows each row can be divide into columns For the square patterns: number of rows and number of columns must be equal Q_1: WAP TO PRINT THE SQUARE PATTERN WITH ROW NUMBER. PATTERN: row_1: 1 1 1 1 row_2: 2 2 2 2 row_3: 3 3 3 3 row_4: 4 4 4 4 class SquarePattern{ public static void main(String[] args) { // outer loop is for getting rows for(int row = 1;row <= 5;row++) { // inner loop for getting the columns for(int col = 1;col <= 5;++col) { System.out.print(row+"\t"); } System.out.println(); } } } ================================================= Pattern_2: ========= Q: WAP IN JAVA TO PRINT A SQUARE PATTERN WITH ITS COLUMN NUMBER. 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 class SquarePattern{ public static void main(String[] args) { // outer loop is for getting rows for(int row = 1;row <= 5;row++) { // inner loop for getting the columns for(int col = 1;col <= 5;++col) { System.out.print(col+"\t"); } System.out.println(); } } } =========================================== Rectangle Patterns: =================== number of rows > number of columns number of rows < number of columns Pattern_3: Q_3) WAP IN JAVA TO PRINT REACTANGLE PATTERN WITH ROW NUMBER. 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 import java.util.Scanner; class RectanglePattern{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); int rows,cols; System.out.println("Enter number of rows and columns of rectangle:"); rows = obj.nextInt(); cols = obj.nextInt(); // outer loop for getting rows for(int r = 1;r <= rows;r++) { // inner loop for getting the columns for(int c = 1;c <= cols;c++) { System.out.print(r+" "); } System.out.print("\n"); } } } ============================================= Q_4) WAP IN JAVA TO PRINT THE RECTANGLE PATTERN WITH '.' import java.util.Scanner; class RectanglePattern{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); int rows,cols; System.out.println("Enter number of rows and columns of rectangle:"); rows = obj.nextInt(); cols = obj.nextInt(); // outer loop for getting rows for(int r = 1;r <= rows;r++) { // inner loop for getting the columns for(int c = 1;c <= cols;c++) { System.out.print("."+"\t"); } System.out.print("\n"); } } } ================================================= Pyramid Patterns: ================= Q_5) WAP IN JAVA TO PRINT RIGHT-ANGLED TRIANGLE PATTERN/PYRAMID PATTERN WITH '*' class PyramidPattern{ public static void main(String[] args) { // outer loop for row-wise operation for(int row = 1;row <= 6;++row) { // inner for loop for column-wise operation for(int col = 1;col <= row;++col) { System.out.print("* "); } System.out.println(); } } } ============================================= Inverted Pyramid Pattern: ========================= 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 class InvertedPyramid{ public static void main(String[] args) { for(int row = 5;row > 0;--row) { for(int col = 1;col <= row;++col) { System.out.print(col+"\t"); } System.out.println(); } } } ==================== class InvertedPyramid{ public static void main(String[] args) { for(int row = 5;row > 0;--row) { for(int col = row;col >= 1;--col) { System.out.print(col+"\t"); } System.out.println(); } } } Assignment: =========== 1) WAP TO PRINT THE SQUARE PATTERN WITH ROW NUMBER. HERE THE NUMBER OF ROWS IN A PATTERN SHOULD BE AN INPUT. SYNTAX: for(row = 1;row <= n;++row) { for(col = 1;col <= n;++col) { } } 2) WAP IN JAVA TO PRINT THE SQUARE PATTERN WITH COLUMN NUMBER. HERE THE NUMBER OF ROWS IN A PATTERN SHOULD BE AN INPUT. 3) WAP IN JAVA TO PRINT REACTANGLE PATTERN WITH Column NUMBER. 4) WAP IN JAVA TO PRINT THE SQUARE PATTERN WITH '*' 5) WAP IN JAVA TO PRINT RIGHT-ANGLED TRIANGLE PATTERN/PYRAMID PATTERN WITH ROW NUMBER. 1 2 2 3 3 3 4 4 4 4