Advertisement
GabrielHr00

01. Number Pyramid

Jun 18th, 2023
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. package S6_NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NumberPyramid {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int num = Integer.parseInt(scanner.nextLine());
  9.  
  10.         boolean isCounterOverLimit = false;
  11.         int counterNum = 1;
  12.         for (int r = 1; r <= num; r++) {
  13.             for (int c = 1; c <= r; c++) {
  14.                 System.out.print(counterNum + " ");
  15.                 counterNum++;
  16.  
  17.                 if (counterNum > num) {
  18.                     isCounterOverLimit = true;
  19.                     break;
  20.                 }
  21.             }
  22.  
  23.             if (isCounterOverLimit) {
  24.                 break;
  25.             }
  26.             System.out.println();
  27.         }
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement