Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SarathKumarMca_SobhanThakur {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- System.out.print("Enter number of lines of output wanted: ");
- final int numberOfOutputRows = sc.nextInt();//For no. of rows
- printNumberTriangle(numberOfOutputRows);
- }
- protected static void printNumberTriangle(final int numberOfOutputRows) {
- int numberToPrint = 0;
- for (int rowNum = 1; rowNum <= numberOfOutputRows; rowNum++) {
- numberToPrint = numberToPrint + rowNum;
- final boolean isAnOddRow = (rowNum % 2 != 0);
- for (int j = 1; j <= rowNum; j++) {
- if (isAnOddRow)
- System.out.print(numberToPrint++ + " ");
- else
- System.out.print(numberToPrint-- + " ");
- }
- System.out.println();
- if (isAnOddRow)
- numberToPrint--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement