thorax232

Number Pyramid

Oct 26th, 2012
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. //4.17
  2. //Prepared by Ethan Glover
  3.  
  4. import java.util.Scanner;
  5. public class Prog4i17 {
  6. public static void main(String[] args) {
  7.  
  8.      //Ask for user input
  9.         Scanner input = new Scanner(System.in);
  10.         System.out.println("Enter the number of lines: ");
  11.         int n = input.nextInt();
  12.  
  13.         //Loop
  14.         for (int i = 1; i <= n; i++) {
  15.                 if (i <= 9) {
  16.                 //Print spaces
  17.                 for (int j = i; j < n; j++)
  18.                         System.out.print("  ");
  19.                 //Print numbers n-1
  20.                 for (int k = i; k >= 1; k--)
  21.                         System.out.print(" " + k);
  22.                 //Print numbers 1+1 - n
  23.                 for (int l = 2; l <= i; l++)
  24.                         System.out.print(" " + l);
  25.                 System.out.println();
  26.                 }
  27.                 else {
  28.                 //Print spaces
  29.                 for (int j = i; j < n; j++)
  30.                         System.out.print("   ");
  31.                 //Print numbers n-1
  32.                 for (int k = i; k >= 1; k--)
  33.                         System.out.print(" " + k);
  34.                 //Print numbers 1+1 - n
  35.                 for (int l = 2; l <= i; l++)
  36.                         System.out.print(" " + l);
  37.                 System.out.println();
  38.                 }
  39.         }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment