Share on Google+Share on Google+

How to Create Create Matrix of any order

Advertisement
In this section, you will learn how to create a matrix of any order Using Java Programming.

Create Matrix of any order Java Program

A matrix is a rectangular array of numbers.The numbers in the matrix are called its entries or its elements. A matrix with m rows and n columns is called an m-by-n matrix or m n matrix, while m and n are called its dimensions.Here we have allowed the user to enter the number of rows and columns and the elements they wish to be in the matrix.

2x2 Matrix:
1 2
3 4
3x3 Matrix:
1 2 3
2 3 4
4 5 6

Here is the code:

import java.io.*;
import java.util.*;

public class matrix {
	public static void main(String[] args) throws IOException {
		int[][] dim = new int[2][2];
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the rows of matrix : ");
		dim[0][0] = Integer.parseInt(stdin.readLine());
		System.out.println("Enter the columns of matrix : ");
		dim[0][1] = Integer.parseInt(stdin.readLine());

		int[][] A = new int[dim[0][0]][dim[0][1]];
		for (int i = 0; i < A.length; i++)
		for (int j = 0; j < A[i].length; j++) {
        System.out.println("Enter element ("+(i+1)+","+(j+ 1)1)+"): ");
		A[i][j] = Integer.parseInt(stdin.readLine());
			}
		System.out.println("Matrix ");
		for (int i = 0; i < A.length; i++) {
			System.out.println();
			for (int j = 0; j < A[i].length; j++) {
				System.out.print(A[i][j] + " ");
			}
		}
	}
}

Advertisements

Posted on: November 9, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.