Advertisement
veronikaaa86

03. Printing Triangle

Jun 8th, 2022
322
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. printTriangle(n);
  11. }
  12.  
  13. public static void printTriangle(int n){
  14. for (int i = 1; i <= n; i++) {
  15. printLine(1, i);
  16. }
  17.  
  18. for (int i = n - 1; i >= 1; i--) {
  19. printLine(1, i);
  20. }
  21. }
  22.  
  23. public static void printLine(int start, int end) {
  24. for (int i = start; i <= end; i++) {
  25. System.out.print(i + " ");
  26. }
  27. System.out.println();
  28. }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement