Advertisement
veronikaaa86

01. Number Pyramid

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