In: Computer Science
Java Programming
Write a program that displays the following pattern
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Printing Pattern A
*
**
***
****
*****
******
*******
Printing Pattern B
*******
******
*****
****
***
**
*
Printing Pattern C
*
**
***
****
*****
******
*******
public class Pattern {
public static void main(String[] args) {
int rows = 4;
int k = 0;
for(int i = 1; i <= rows; ++i, k = 0) {
for(int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
while(k != 2 * i - 1) {
System.out.print("* ");
++k;
}
System.out.println();
}
for(int i = rows-1; i >= 1; --i) {
for(int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
for(int j=i; j <= 2 * i - 1; ++j) {
System.out.print("* ");
}
for(int j = 0; j < i - 1; ++j) {
System.out.print("* ");
}
System.out.println();
}
System.out.println("Printing Pattern A");
int rowA = 7;
for (int i = 1; i <= rowA; i++) {
for (int j = 0; j < i ; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("Printing Pattern B");
int rowB = 7;
for (int i = rowB; i >= 1; i--) {
for (int j = 0; j < i ; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("Printing Pattern C");
int rowC = 7;
for (int i = 1; i <= rowC; i++) {
for (int j = 0; j < i ; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
/* OUTPUT */
/* PLEASE UPVOTE */