Advertisement
veronikaaa86

03. Printing Triangle

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