Java Pattern Programs
Write a program in Java to display the following pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
public class PatternProgram
{
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();
}
}
}
Write a program in Java to display the following pattern:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
public class PatternProgram
{
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 the program in Java to display the following pattern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
public class PatternProgram
{
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();
}
}
}
Write the program in Java to display the following pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
public class PatternProgram
{
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();
}
}
}
Write a program in Java to display the following pattern:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
public class PatternProgram
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Write the program in Java to display the following pattern:
1 3 5 7 9
1 3 5 7
1 3 5
1 3
1
public class PatternProgram
{
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();
}
}
}
Write the program in Java to display the following pattern:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
public class PatternProgram
{
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();
}
}
}
Write the program in Java to display the following pattern:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
public class PatternProgram
{
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();
}
}
}
Write a program in Java to display the following pattern:
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
public class PatternProgram
{
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();
}
}
}
Write a program in Java to display the following pattern:
3
44
555
6666
77777
public class PatternProgram
{
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 the program in Java to display the following pattern:
9 9 9 9 9
7 7 7 7 7
5 5 5 5 5
3 3 3 3 3
1 1 1 1 1
public class PatternProgram
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 1; j <= 5; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
Write the program in Java to display the following pattern:
9
7 9
5 7 9
3 5 7 9
1 3 5 7 9
public class PatternProgram
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = i; j <= 9; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Write the program in Java to display the following pattern:
9
9 7
9 7 5
9 7 5 3
9 7 5 3 1
public class PatternProgram
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 9; j >= i; j -= 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Write a program in Java to display the following pattern:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
NSWER
public class PatternProgram
{
public void displayPattern() {
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 pattern:
1
11
101
1001
public class PatternProgram
{
public static void main(String args[]) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i)
System.out.print(1);
else
System.out.print(0);
}
System.out.println();
}
}
}
Write a program in Java to display the following pattern:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
public class PatternProgram
{
public static void main(String args[]) {
for (int i = 1, j = 1; j <= 5; j++, i += 2) {
for (int k = j; k <= i; k++)
System.out.print(k + " ");
System.out.println();
}
}
}
Write a program in Java to display the following pattern:
1
12
123
1234
12345
public class PatternProgram
{
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 4; j >= i; j--)
System.out.print(" ");
for (int k = 1; k <= i; k++)
System.out.print(k);
System.out.println();
}
}
}
Write a program in Java to display the following pattern:
1
21
321
4321
54321
public class PatternProgram
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = 4; j >= i; j--)
System.out.print(" ");
for (int k = i; k >= 1; k--)
System.out.print(k);
System.out.println();
}
}
}
Write a program in Java to display the following pattern:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
public class PatternProgram
{
public void displayPattern() {
int a = 1;
for (int i = 5; i > 0; 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 pattern:
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
public class PatternProgram
{
public void displayPattern() {
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print(b + " ");
else
System.out.print(a + " ");
}
System.out.println();
}
}
}
Write a program in Java to display the following pattern:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
public class PatternProgram
{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j < i; j++)
System.out.print(i + " ");
for (int k = i; k <= 5; k++)
System.out.print(k + " ");
System.out.println();
}
}
}
No comments: