veronikaaa86

03. Printing Triangle

Feb 1st, 2023
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 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 numInput = Integer.parseInt(scanner.nextLine());
  10.  
  11.         printTriangle(numInput);
  12.     }
  13.  
  14.     public static void printTriangle(int num) {
  15.         for (int i = 1; i <= num; i++) {
  16.             printLine(1, i);
  17.         }
  18.  
  19.         for (int i = num - 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.         System.out.println();
  29.     }
  30. }
  31.  
Add Comment
Please, Sign In to add comment