Advertisement
Spocoman

01. Number Pyramid

Aug 30th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class NumberPyramid {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int num = Integer.parseInt(scanner.nextLine()),
  7.                 counter = 1;
  8.  
  9.         for (int row = 1; row <= num; row++) {
  10.             for (int col = 0; col < row; col++) {
  11.                 if (counter > num) {
  12.                     break;
  13.                 }
  14.                 System.out.printf("%d ", counter);
  15.                 counter++;
  16.             }
  17.             if (counter > num) {
  18.                 break;
  19.             }
  20.             System.out.println();
  21.         }
  22.     }
  23. }
  24.  
  25. ИЛИ:
  26.  
  27. import java.util.Scanner;
  28.  
  29. public class NumberPyramid {
  30.     public static void main(String[] args) {
  31.         Scanner scanner = new Scanner(System.in);
  32.         int num = Integer.parseInt(scanner.nextLine()),
  33.                 counter = 1;
  34.  
  35.         for (int row = 1; row <= num || counter <= num; row++) {
  36.             for (int col = 0; col < row && counter <= num; col++) {
  37.                 System.out.printf("%d ", counter++);
  38.             }
  39.             System.out.println();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement