Skip to content

Instantly share code, notes, and snippets.

@213edu
Last active August 29, 2015 14:06
Show Gist options
  • Save 213edu/bc2ea2dd31a764ff7b81 to your computer and use it in GitHub Desktop.
Save 213edu/bc2ea2dd31a764ff7b81 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
/**
* Created by Ryan on 9/26/2014.
*/
public class Pyramid {
public static void main(String[] args) {
Scanner input;
int value,n;
input = new Scanner(System.in);
System.out.println("Enter the number of rows:");
value = input.nextInt();
for(int i = 0; i < value; i++){
//print spaces in front of the numbers
for (int s = 0; s < value - 1 - i; s++){
System.out.print(" ");
}
//n as the first number in the line; decrease until they reach 1 in the middle.
n = i+1;
while (n > 1){
if (n < 10) {
System.out.print(" "+ n + " ");
}else{
System.out.print(n + " ");
}
n--;
}
//numbers increase until reach the input value
while(n <= i+1){
if (n<10) {
System.out.print(" " + n + " ");
}else{
System.out.print(n + " ");
}
n++;
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment