Computer Applications
Write two separate programs to generate the following patterns using iteration (loop) statements:
(a)
*
* #
* # *
* # * #
* # * # *
(b)
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Java
Java Nested for Loops
ICSE 2015
30 Likes
Answer
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
}
System.out.println();
}
}
}
public class KboatPattern2
{
public static void main(String args[]) {
for (int i = 1; i <=5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output


Answered By
16 Likes
Related Questions
Write a program in Java to find the sum of the following series:
S = 1 + (3/2!) + (5/3!) + (7/4!) + ……. to n
Write a program in Java to display the following pattern:
AAAAA
BBBB
CCC
DD
EWrite a program in Java to display the following pattern:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1Write a program in Java to find the sum of the given series:
S = 1! + 2! + 3! + …. + n!