Silviyaa

NumberPyramid

Mar 14th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class P01NumberPyramid {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.  
  9.         int currentNum = 0;
  10.         for (int rows = 1; rows <= n; rows++) {
  11.             for (int cols = 1; cols <= rows; cols++) {
  12.                 currentNum++;
  13.  
  14.                 if (currentNum > n){
  15.                     break;
  16.                 }
  17.  
  18.                 System.out.print(currentNum + " ");
  19.             }
  20.             if (currentNum > n){
  21.                 break;
  22.             }
  23.             System.out.println();
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment