I need to get an integer from a user and output a pyramid with that number of levels eg. if the person entered 7 it would output
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
the problem I'm having is keeping the 1 in the center. Can anyone figure out how to do this? Here is my code so far:
package javaapplication6;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class JavaApplication6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of lines :");
int num = input.nextInt();
String output = "";
for(int i = 1; i <= num; i++){
output = i != 1 ? i + " " + output + " " + i : "1";
System.out.println(output);
}
}
}