Nested loop pattern
Write a program in Java to display the following pattern:
1
22
333
4444
55555
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++)
System.out.print(i + " ");
System.out.println();
}
}
}
54321
4321
321
21
1
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print( j + " ");
System.out.println();
}
}
}
1
21
321
4321
54321
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = i; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
12345
1234
123
12
1
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
54321
5432
543
54
5
public class KboatPattern
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
13579
1357
135
13
1
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 1; j <= i; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
5
54
543
5432
54321
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
12345
2345
345
45
5
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = i; j <= 5; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
5
44
333
2222
11111
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5, j = 1; i >= 1; i--, j++) {
for (int k = 1; k <= j; k++)
System.out.print(i + " ");
System.out.println();
}
}
}
3
44
555
6666
77777
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 3; i <= 7; i++) {
for (int j = 3; j <= i; j++)
System.out.print(i);
System.out.println();
}
}
}
Write a program to print the series given below.
5 55 555 5555 55555 555555
Answer
public class Pattern
{
public static void main(String args[]) {
for (int i = 1; i <= 6; i++) {
for (int j = 0; j < i; j++) {
System.out.print('5');
}
System.out.print(' ');
}
}
}
Write a program in Java to display the following patterns.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Answer
public class Pattern
{
public static void main(String args[]) {
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}
Write a program in Java to display the following patterns.
ABCDE
ABCD
ABC
AB
A
Answer
public class Pattern
{
public static void main(String args[]) {
for (int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
System.out.print((char)j);
}
System.out.println();
}
}
}
Write a program in Java to display the following patterns.
54321
4321
321
21
1
Answer
public class Pattern
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print( j + " ");
System.out.println();
}
}
}
Write a program in Java to display the following patterns.
J
JA
JAV
JAVA
Answer
public class Pattern
{
public static void main(String args[]) {
String str = "JAVA";
int len = str.length();
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
System.out.print(str.charAt(j) + " ");
}
System.out.println();
}
}
}
Write a program to generate a triangle or an inverted triangle till n terms based upon the
user's choice of triangle to be displayed.
Example 1
Input:
Type 1 for a triangle and type 2 for an inverted triangle
1
Enter the number of terms
5
Output:
1
22
333
4444
55555
Example 2
Input:
Type 1 for a triangle and type 2 for an inverted triangle
2
Enter the number of terms
6
Output:
666666
55555
4444
333
22
1
Answer
import java.util.Scanner;
public class Pattern
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = n; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}