> Write a program to print the following pattern in java 1 1 2 1 1 2 3 2 1?

Write a program to print the following pattern in java 1 1 2 1 1 2 3 2 1?

Posted at: 2014-12-18 
import java.io.*;

import java.util.*;

public class Program {

public static void main(String []args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter a number: ");

int n=in.nextInt();

for(int i=1;i<=n;i++) {

for(int j=i;j<=n-1;j++) {

System.out.print(" ");

}

for(int j=1;j<=i;j++) {

System.out.print(j);

}

for(int j=i-1;j>=1;j--) {

System.out.print(j);

}

System.out.println();

}

}

}

Output:

$ java Program

Enter a number: 4

1

121

12321

1234321

Now you've had time to work this out for yourself, if you're still stuck, try something like this, which uses nested for loops. Good luck!

public class PatternMaker {

public static void main (String[] args) {

int i, j;

// outer loop counts up from 1 to 3

for (i=1; i<=3; i++) {

for (j=1; j<=i; j++) { // inner loop counts up

System.out.print(j+" ");

} // end for j

for (j=i-1; j>=1; j--) { // inner loop counts down

System.out.print(j+" ");

} // end for j

} // end for i

} // end main()

} // end class PatternMaker

System.out.println("1 1 2 1 1 2 3 2 1");