by Dinesh Thakur

 

import java.io.*;

class NxNMatrix
{
            public static void main(String args[]) throws IOException
            {
                        BufferedReader gg=new BufferedReader(new InputStreamReader(System.in));
                        int i,j,r,c;
                        String m;
                        System.out.print("How many Rows ? ");
                        m=gg.readLine();
                        r=Integer.parseInt(m);
                        System.out.print("How many Columns ? ");
                        m=gg.readLine();
                        c=Integer.parseInt(m);
                        int h[] []=new int[r] [c];
                        System.out.println("Enter elements for matrix of order "+r+" x "+c);
                        for(i=0;i<=r-1;i++)
                        {
                                    for(j=0;j<=c-1;j++)
                                    {
                                                m=gg.readLine();
                                                h[i] [j]=Integer.parseInt(m);
                                    }
                        }
                        System.out.println("Elements in matrix are ");
                        for(i=0;i<=r-1;i++)
                        {
                                    for(j=0;j<=c-1;j++)
                                    {
                                                System.out.print(h[i][j]+"\t");
                                    }
                                    System.out.println();
                        }
            }
}

nxn Matrix in Java Example