Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MultiplicationTriangle {
  4.  
  5.     public MultiplicationTriangle() {
  6.         int rowsEntered;
  7.  
  8.         System.out.println("Multiplication Triangle");
  9.         printTriangle(4); // print one example
  10.  
  11.         do {
  12.             Scanner input = new Scanner(System.in);
  13.             System.out.print("Enter the number of rows you would like to print: ");
  14.             rowsEntered = Integer.parseInt(input.nextLine());
  15.             printTriangle(rowsEntered);
  16.         } while (rowsEntered > 0);
  17.         System.out.println("Thank you for using this program!");
  18.  
  19.     }
  20.  
  21.     private void printTriangle(int maxRows) {
  22.         System.out.println("\nNumber of rows for this triangle: " + maxRows);
  23.         for (int column = 1; column <= maxRows; column++) { // print header
  24.             System.out.print(String.format("%4d", column));
  25.         }
  26.         System.out.println();
  27.  
  28.         for (int row = 1; row <= maxRows; row++) {
  29.             for (int column = 1; column <= row; column++) {
  30.                 System.out.print(String.format("%4d", column * row));
  31.             }
  32.             System.out.println();
  33.         }
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.         new MultiplicationTriangle();
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement