Categories
Java

Matrix Generation Using Array

The following java program generates matrix using two dimensional array.

import java.io.*;
class Matrix
{
int r,c;
int a[][];
BufferedReader br;
void get()throws Exception
{
System.out.println("Enter number of rows and columns");
br=new BufferedReader(new InputStreamReader(System.in));
r=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
a=new int[r][c];
}
void pro()throws Exception
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
System.out.println("Enter element");
a[i][j]=Integer.parseInt(br.readLine());
}
}
void show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print("\t"+a[i][j]);
System.out.println(" ");
}
}
}
class MatrixMain
{
public static void main(String arg[])throws Exception
{
Matrix obj=new Matrix();
obj.get();
obj.pro();
obj.show();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *