Advertisement
GabrielHr00

NumberPyramid

Dec 5th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 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.  
  8. // save current num and track
  9. int currentNum = 1;
  10. for(int r = 1; r <= num; r++) {
  11. //check for value of current num
  12. boolean isCurrentNum = false;
  13. for (int c = 1; c <= r; c++) {
  14. // print the current num and increment for next step
  15. System.out.print(currentNum + " ");
  16. currentNum++;
  17.  
  18. // check if current num is greater than the num above
  19. if(currentNum > num) {
  20. // set the flag on true
  21. isCurrentNum = true;
  22. break;
  23. }
  24. }
  25.  
  26. if(isCurrentNum) {
  27. break;
  28. }
  29. System.out.println();
  30. }
  31.  
  32. }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement