PRINT * PATTERNS USING LOOPS
Pattern 1
package com.cnc;
public class Main {
public static void main(String[] args) {
int rows = 5;
// TO PRINT i ROWS
for(int i = 1; i <= rows; ++i) {
// TO PRINT j COLUMN
for(int m = 1; i <= j; ++j) {
System.out.print(" *");
}
System.out.println();
}
}
OUTCOME:
*
* *
* * *
* * * *
* * * * *
Pattern 2
package com.cnc;
public class Main {
public static void main(String[] args) {
int rows1 = 5;
for(int i = rows1; i>=1;i--){
for(int j = 1;j<=i;j++) {
System.out.print(" *");
}
System.out.println("");
}
}
}
OUTCOME:
* * * * * * * * * * * * * * *
Pattern 3
* * * * * Here _ is the blank and general formula for i th rows the _ is 2(i-1) _ * * * * and for number of * for i th row is n-i+1 , n= rows _ _ * * * _ _ _ * * _ _ _ _ *
public static void main(String[] args) {
int rows1 = 5;
for(int i = 1; i<=rows1;i++){
for(int j = 1;j<=(i-1);j++) { //not using 2(i-1) rather we will
System.out.print(" "); // be using the space 2 times to avoid complication
}
for(int j = 1;j<=rows1-i+1;j++){
System.out.print("* ");
}
System.out.println();
}
}}
Pattern 4