Advertisement
JeffGrigg

SarathKumarMca_SobhanThakur

Aug 17th, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SarathKumarMca_SobhanThakur {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc=new Scanner(System.in);
  7.         System.out.print("Enter number of lines of output wanted: ");
  8.         final int numberOfOutputRows = sc.nextInt();//For no. of rows
  9.         printNumberTriangle(numberOfOutputRows);
  10.     }
  11.  
  12.     protected static void printNumberTriangle(final int numberOfOutputRows) {
  13.         int numberToPrint = 0;
  14.         for (int rowNum = 1; rowNum <= numberOfOutputRows; rowNum++) {
  15.             numberToPrint = numberToPrint + rowNum;
  16.             final boolean isAnOddRow = (rowNum % 2 != 0);
  17.             for (int j = 1; j <= rowNum; j++) {
  18.                 if (isAnOddRow)
  19.                     System.out.print(numberToPrint++ + " ");
  20.                 else
  21.                     System.out.print(numberToPrint-- + " ");
  22.             }
  23.             System.out.println();
  24.             if (isAnOddRow)
  25.                 numberToPrint--;
  26.         }
  27.     }
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement