1.
public class Main {
public static void main(String[] args) {
int num = 1;
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 6 - row; col++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Output
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
2.
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 5; row++) {
char ch = 'A';
for (int col = 1; col <= 6 - row; col++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
}
}
Output
A B C D E
A B C D
A B C
A B
A
3.
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 5; row++) {
char ch = (char)('F' - row);
for (int col = 1; col <= 6 - row; col++) {
System.out.print(ch + " ");
ch--;
}
System.out.println();
}
}
}
Output
E D C B A
D C B A
C B A
B A
A
4.
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 5 - row; col++) {
System.out.print(col + " ");
}
System.out.print("*");
System.out.println();
}
}
}
Output
1 2 3 4 *
1 2 3 *
1 2 *
1 *
*
5.
Top comments (0)