Advertisement
dimipan80

Matrix of Numbers

Aug 8th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. /* Write a program that reads from the console a positive integer number n (1 ≤ n ≤ 20)
  2.  * and prints a matrix. Use two nested loops. */
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class _09_MatrixOfNumbers {
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Scanner scan = new Scanner(System.in);
  11.         System.out.print("Enter a whole number in the range [1 .. 20] for N: ");
  12.         int numN = scan.nextInt();
  13.         scan.close();
  14.  
  15.         if (numN > 0 && numN <= 20) {
  16.             System.out.println("The Matrix of Numbers is:");
  17.             for (int row = 1; row <= numN; row++) {
  18.                 for (int col = row; col < row + numN; col++) {
  19.                     System.out.printf("%3d", col);
  20.                 }
  21.  
  22.                 System.out.println();
  23.             }
  24.         } else {
  25.             System.out.println("Error! - Invalid Input number!!!");
  26.         }
  27.     }
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement