In: Computer Science
. Write a program to print * in the following order using 2d array in java
* * * * *
* * * *
* * *
* *
*
JAVA CODE:
// printClass class
public class printClass {
public static void main(String[] args) {
// 2D array is initialized
String[][] array = new String[5][5];
// empty string " " is assigned to all the values in the array
for (int i = 0; i < 5; i++) {
for (int j= 0; j < 5; j++) {
array[i][j] = " ";
}
}
// '*' is assigned to first upper half of the triangle in the array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5-i; j++) {
array[i][j] = "*";
}
}
// values stored in the array are printed
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(array[i][j] + " ");
}
System.out.print("\n");
}
}
}
OUTPUT: