Java Arrays 2D

import java.util.Scanner;

public class Array2d {

  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int rows,cols;

    System.out.print("Enter number of rows: ");

    rows = sc.nextInt();

    System.out.print("Enter number of columns: ");

    cols = sc.nextInt();

    int[][] num = new int[rows][cols];

    for (int i = 0; i < num.length; i++) {

      for (int j = 0; j < num[0].length; j++) {

          System.out.println(

            String.format("Enter value for [%d][%d]",i,j));

          num[i][j] = sc.nextInt();

      }

    }



    for (int i = 0; i < num.length; i++) {

      for (int j = 0; j < num[0].length; j++) {

          System.out.print(num[i][j]);

        System.out.print(" ");

      }

      System.out.println("");

    }



    System.out.println();

       System.out.println(num[0].length);

  }

 

}

Comments

Popular posts from this blog

Hackerrank - Quicksort 2 - Sorting

Hackerrank - Day of the Programmer

Hackerrank - UNIQUE ARMSTRONG NUMBER