Java Number Pattern Example Programs

Pattern1


public class Pattern1 {
/*
* 1
* 12
* 123
* 1234
* 12345
*/
public static void main(String[] args) {
// i means number of rows to print
for (int i = 1; i <= 5; i++) {
//j means number of columns to print
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
<
System.out.println();
}

}

}

output


Pattern 2


public class Pattern1 {
/*
* 1
* 22
* 333
* 4444
* 55555
*/
public static void main(String[] args) {
// i means number of rows to print
for (int i = 1; i <= 5; i++) {
//j means number of columns to print
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}

}

}

output


Pattern 3


public class Pattern3 {
/*55555
4444
333
22
1
*/

public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}

output


Pattern 4


public class Pattern4 {
/*12345
1234
123
12
1
*/

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();
}

}
}

output


Pattern 5


public class Pattern5 {
/*11111
2222
333
44
5
*/
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(i);
}
System.out.println();
}
}
}

output

No comments:

Post a Comment