ICSE Class 10 Computer Applications
Question 4 of 22
Nested for loops — Question 4
Back to all questions 4
Question Question 2(iii)
Write a program in Java to display the following patterns.
#
* *
# # #
* * * *
# # # # #
public class KboatPattern
{
public static void main(String args[]) {
char ch1 = '#', ch2 = '*';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (i % 2 == 0)
System.out.print(ch2 + " ");
else
System.out.print(ch1 + " ");
}
System.out.println();
}
}
}Output
