Showing Element of Matrix

import java.util.Scanner;
public class ShowingElementOfMatrix
{
void through_user_input()   //method one 
{
Scanner sc = new Scanner(System.in);
int a[][] = new int[2][2];
System.out.println("Enter the elements of matrix");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
int n = sc.nextInt();
System.out.println("Element of Matrix at[" + i + "][" + j + "] is " + n);
}
}
}
void predefined_element()   //method second
{
int a[][] = {{2, 4}, {5, 6}};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
System.out.println("Element of Matrix at[" + i + "][" + j + "] is " + a[i][j]);
}
}
}

void transpose_of_predefined_matrix()                    //method third
{
int a[][] = {{2, 4}, {5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.println("Element of Matrix at[" + i + "][" + j + "] is " + a[j][i]);
}}
}



public static void main(String[] args)
{
ShowingElementOfMatrix ob = new ShowingElementOfMatrix();
ob.through_user_input();
System.out.println("The value at predefined matrix are");
ob.predefined_element();
System.out.println("Transpose of predefined matrix is");
ob.transpose_of_predefined_matrix();
}
}

                                     OUTPUT

Enter the elements of matrix
2
Element of Matrix at[0][0] is 2
4
Element of Matrix at[0][1] is 4
5
Element of Matrix at[1][0] is 5
6
Element of Matrix at[1][1] is 6
The value at predefined matrix are
Element of Matrix at[0][0] is 2
Element of Matrix at[0][1] is 4
Element of Matrix at[1][0] is 5
Element of Matrix at[1][1] is 6

Transpose of predefined matrix is
Element of Matrix at[0][0] is 2
Element of Matrix at[0][1] is 5
Element of Matrix at[1][0] is 4
Element of Matrix at[1][1] is 6

No comments:

Post a Comment