Matrix Programs
Matrix Programs
Matrix
Question
PROGRAM TO CALCULATE THE ADDITION OF 2 MATRICES
MATRIX A = [1, 1, 1]
[4, 5, 6]
[1, 2, 3]
MATRIX B = [1, 1, 1]
[-2, 3, 1]
[1, 0, 1]
import java.util.Scanner;
public class SumOfTwoMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][],b[][],sum[][];
a=new int[row][col];
b=new int[row][col];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
}
}
Question
PROGRAM TO SUBTRACT TWO MATRICES
MATRIX A = [4, 5, 5]
[3, 0, 1]
[1, 2, 3]
MATRIX B = [2, 0, 3]
[2, 3, 1]
[0, 1, 1]
import java.util.Scanner;
public class SubtractTwoMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][],b[][],subtract[][];
a=new int[row][col];
b=new int[row][col];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
subtract[i][j]=a[i][j]-b[i][j];
}
}
}
}
Question
PROGRAM TO DETERMINE WHETHER A GIVEN MATRIX IS AN IDENTITY MATRIX.
A MATRIX IS SAID TO BE THE IDENTITY MATRIX IF IT IS THE SQUARE MATRIX IN WHICH
ELEMENTS OF PRINCIPLE DIAGONAL ARE ONES, AND THE REST OF THE ELEMENTS ARE ZEROES.
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
import java.util.Scanner;
public class identityMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0;
int a[][];
boolean flag=true;
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
System.out.print("("+(i+1)+","+(j+1)+"):");
a[i][j]=sc.nextInt();
}
}
System.out.println("MATRIX:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
if(i==j)
{
if(a[i][j]!=1)
{
flag=false;
break;
}
}
else if(i!=j)
{
if(a[i][j]!=0)
{
flag=false;
break;
}
}
}
}
if(flag==true)
{
System.out.println("MATRIX IS AN IDENTITY MATRIX");
}
else
{
System.out.println("MATRIX IS NOT AN IDENTITY MATRIX");
}
}
}
Question
PROGRAM TO DETERMINE WHETHER A GIVEN MATRIX IS A SPARSE MATRIX.
A MATRIX IS SAID TO BE A SPARSE MATRIX IF MOST OF THE ELEMENTS OF THAT MATRIX ARE 0. IT IMPLIES THAT
IT CONTAINS VERY FEW NON-ZERO ELEMENTS.
FOR THE MATRIX TO BE SPARSE, MORE THAN HALF OF THE ELEMENTS PRESENT IN THE MATRIX MUST BE ZERO.
import java.util.Scanner;
public class sparseMatrix
{
public static void main(String[] args)
{
int row=0, col=0,count=0,size=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
//counting total number of zeroes
if(a[i][j]==0)
{
count++;
}
}
}
System.out.println("MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
size=row*col;
if(count>(size/2))
{
System.out.println("MATRIX IS A SPARSE MATRIX");
}
else
{
System.out.println("MATRIX IS NOT A SPARSE MATRIX");
}
}
}
Question
PROGRAM TO DETERMINE WHETHER TWO MATRICES ARE EQUAL
MATRIX A:
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
MATRIX B:
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
BOTH MATRICES ARE EQUAL
Answer
import java.util.Scanner;
public class equalMatrix
{
public static void main(String[] args)
{
int row=0, col=0,rowb=0,colb=0,i=0,j=0;
int a[][],b[][];
boolean flag=true;
if(row!=rowb||col!=colb)
{
System.out.println("BOTH MATRICES ARE NOT EQUAL");
}
else
{
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX A");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
b=new int[rowb][colb];
System.out.println("ENTER THE ELEMENTS IN MATRIX B");
for(i=0;i< rowb;i++)
{
for(j=0;j< colb;j++)
{
b[i][j]=sc.nextInt();
}
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]!=b[i][j])
{
flag=false;
break;
}
}
}
if(flag==false)
{
System.out.println("TWO MATRICES ARE NOT EQUAL");
}
else
{
System.out.println("TWO MATRICES ARE EQUAL");
}
}
}
}
Question
PROGRAM TO DISPLAY THE LOWER TRIANGULAR MATRIX
(LOWER TRIANGULAR MATRIX IS A SQUARE MATRIX IN WHICH ALL THE ELEMENTS ABOVE THE
PRINCIPLE DIAGONAL WILL BE ZERO)
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class LowerTriangularMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][];
if(row!=col)
{
System.out.println("MATRIX NEED TO BE SQUARE MATRIX FIRST!!!");
}
else
{
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
/*if j > i then print 0 else print number present in array at that position */
System.out.println("LOWER TRIANGULAR MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(j > i)
{
System.out.print("0 ");
}
else
{
System.out.print(a[i][j]+" ");
}
}
System.out.println();
}
}
}
Question
PROGRAM TO DISPLAY THE UPPER TRIANGULAR MATRIX
(UPPER TRIANGULAR MATRIX IS A SQUARE MATRIX IN WHICH ALL THE ELEMENTS BELOW THE
PRINCIPLE DIAGONAL WILL BE ZERO)
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class UpperTriangularMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][];
if(row!=col)
{
System.out.println("MATRIX NEED TO BE SQUARE MATRIX FIRST!!!");
}
else
{
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
/*if i > j then print 0 else print number present in array at that position */
System.out.println("UPPER TRIANGULAR MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i > j)
{
System.out.print("0 ");
}
else
{
System.out.print(a[i][j]+" ");
}
}
System.out.println();
}
}
}
Question
PROGRAM TO DISPLAY THE EVEN NUMBER IN THE MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class EvenNumberInMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
/*if a[i][j]%2==0 then print number present in array at that position else print spaces
*/
System.out.println("EVEN NUMBER IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]%2==0)
{
System.out.print(a[i][j]+" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Question
PROGRAM TO DISPLAY THE ODD NUMBER IN THE MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class OddNumberInMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
/*if a[i][j]%2!=0 then print number present in array at that position else print spaces */
System.out.println("ODD NUMBER IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]%2!=0)
{
System.out.print(a[i][j]+" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Question
FIND THE FREQUENCY OF ODD NUMBER ELEMENTS IN THE GIVEN MATRIX.
import java.util.Scanner;
public class FrequencyOfOddNumberInMatrix
{
public static void main(String[] args)
{
int row=0, col=0,count=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
/*if a[i][j]%2!=0 then increment 'count' by 1*/
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]%2!=0)
{
count++;
}
}
System.out.println("FREQUENCY OF ODD NUMBER IN MATRIX: "+count);
}
}
Question
PROGRAM TO PRINT FREQUENCY OF EVEN AND ODD NUMBER IN THE MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class FrequencyOfEvenAndOddNumberInMatrix
{
public static void main(String[] args)
{
int row=0, col=0,even=0,odd=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]%2==0)
{
even++;
}
else
{
odd++;
}
}
}
System.out.println("FREQUENCY OF EVEN NUMBER IN MATRIX: "+even);
System.out.println("FREQUENCY OF ODD NUMBER IN MATRIX: "+odd);
}
}
Question
MULTIPLY TWO INTEGER MATRICES AND PRINT BOTH THE ORIGINAL MATRICES AND
RESULTANT MATRIX.
import java.util.Scanner;
public class MatrixMultiplication
{
public static void main()
{
int[ ][ ] matrix1,matrix2,ProductOfFirstAndSecondMatrix;
int row1,col1,row2,col2,product,sum=0;
int i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Row size of 1st matrix:");
row1=sc.nextInt();
System.out.println("Enter Column size of 1st matrix:");
col1=sc.nextInt();
System.out.println("Enter Row size of 2nd matrix:");
row2=sc.nextInt();
System.out.println("Enter Column size of 2nd matrix:");
col2=sc.nextInt();
if(col1!=row2)
{
System.out.println("Column of the First matrix should be equal to row of second
matrix");
}
else
{
matrix1=new int[row1][col1];
matrix2=new int[row2][col2];
ProductOfFirstAndSecondMatrix=new int[row1][col2];
System.out.println("Enter Elements in First Matrix:");
for(i=0;i< row1;i++)
{
for(j=0;j< col1;j++)
{
matrix1[i][j]=sc.nextInt();
}
}
System.out.println("Enter Elements in Second Matrix:");
for(i=0;i< row2;i++)
{
for(j=0;j< col2;j++)
{
matrix2[i][j]=sc.nextInt();
}
}
for(i=0;i< row1;i++)
{
for(j=0;j< col2;j++)
{
k=0;
while(k< col1)
{
product=matrix1[i][k]*matrix2[k][j];
sum=sum+product;
k++;
}
ProductOfFirstAndSecondMatrix[i][j]=sum;
sum=0;
}
System.out.println("Matrix 1:");
for(i=0;i< row1;i++)
{
for(j=0;j< col1;j++)
{
System.out.print(matrix1[i][j]+" ");
}
System.out.println();
}
System.out.println("Matrix 2:");
for(i=0;i< row2;i++)
{
for(j=0;j< col2;j++)
{
System.out.print(matrix2[i][j]+" ");
}
System.out.println();
}
System.out.println("Matrix 1 * Matrix 2:");
for(i=0;i< row1;i++)
{
for(j=0;j< col2;j++)
{
System.out.print(ProductOfFirstAndSecondMatrix[i][j]+" ");
}
System.out.println();
}
}
}
}
Question
PROGRAM TO FIND SUM OF NUMBERS IN THE MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SUM OF ALL NUMBER IN THE MATRIX:45
Answer
import java.util.Scanner;
public class SumOfNumberInMatrix
{
public static void main(String[] args)
{
int row=0, col=0,sum=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
sum=sum+a[i][j];
}
}
System.out.println("SUM OF ALL NUMBERS IN THE MATRIX: "+sum);
Question
PROGRAM TO FIND SUM OF EACH COLUMN IN THE MATRIX
MATRIX =
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class SumOfEachColumnInMatrix
{
public static void main(String[] args)
{
int row=0, col=0,sum=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
}
System.out.println(sum);
sum=0;
}
}
}
Question
PROGRAM TO FIND THE TRANSPOSE OF A GIVEN MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
TRANSPOSE
[9, 6, 3]
[8, 5, 2]
[7, 4, 1]
Answer
import java.util.Scanner;
public class TransposeMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][],t[][];
a=new int[row][col];
t=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
t[i][j]=a[j][i];
}
System.out.println("TRANSPOSE OF MATRIX: ");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(t[i][j]);
}
System.out.println();
}
}
Question
PROGRAM TO CHECK WHETHER THE MATRIX IS SYMMETRIC MATRIX
(SYMMETRIC MATRIX IS A SQUARE MATRIX THAT IS EQUAL TO ITS TRANSPOSE)
MATRIX:
[3, -2, 4]
[-2, 6, 2]
[4, 2, 3 ]
TRANSPOSE:
[3, -2, 4]
[-2, 6, 2]
[4, 2, 3]
THE MATRIX IS SYMMETRIC MATRIX
Answer
import java.util.Scanner;
public class symmetricMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][],t[][];
boolean flag=true;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
t[i][j]=a[j][i];
/*checking if element of transpose matrix is not equal to element original
matrix at positions 'i' and 'j'*/
if(t[i][j]!=a[i][j])
{
flag=false;
}
if(flag==false)
{
System.out.println("MATRIX IS NOT SYMMETRIC MATRIX");
}
else
{
System.out.println("MATRIX IS SYMMETRIC MATRIX");
}
}
}
Question
PROGRAM TO CHECK WHETHER THE MATRIX IS AN ORTHOGONAL MATRIX
(TO DETERMINE IF A MATRIX IS ORTHOGONAL, WE NEED TO MULTIPLY THE MATRIX BY IT'S
TRANSPOSE, AND SEE IF WE GET THE IDENTITY MATRIX.)
MATRIX:
[1,0,0]
[0,1,0]
[0,0,1]
TRANSPOSE:
[1,0,0]
[0,1,0]
[0,0,1]
PRODUCT OF TRANSPOSE MATRIX AND ORIGINAL MATRIX:
[1,0,0]
[0,1,0]
[0,0,1]
MATRIX IS ORTHOGONAL MATRIX
Answer
import java.util.Scanner;
public class OrthogonalMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,sum=0;
int a[][],t[][],product[][];
boolean flag=true;
System.out.print(product[i][j]+" ");
}
System.out.println();
}
/*checking whether the product is identity matrix.In identity matrix, all elements except
on principal diagonal are zero and elements on principal diagonal are 1*/
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if((i==j) &&product[i][j]!=1)
{
flag=false;
break;
}
if(i!=j && product[i][j]!=0)
{
flag=false;
break;
}
}
}
if(flag==false)
{
System.out.println("MATRIX IS NOT ORTHOGONAL MATRIX");
}
else
{
System.out.println("MATRIX IS AN ORTHOGONAL MATRIX");
}
}
}
Question
PROGRAM TO PRINT RIGHT AND LEFT DIAGONALS OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
DIAGONALS OF MATRIX
9 7
5
3 1
Answer
import java.util.Scanner;
public class printDiagonalOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0;
int a[][];
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
System.out.print("("+(i+1)+","+(j+1)+"):");
a[i][j]=sc.nextInt();
}
}
System.out.println("MATRIX:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("DIAGONALS OF MATRIX:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
if(i==j || (i+j==size-1))
{
System.out.print(a[i][j]+" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Question
PROGRAM TO NON-DIAGONAL ELEMENT OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class NonDiagonalElementOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0;
int a[][];
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Question
PROGRAM TO FIND SUM OF NON-DIAGONAL ELEMENTS OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class SumOfNonDiagonalElementsOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0,sum=0;
int a[][];
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
}
}
}
System.out.println("SUM OF NON-DIAGONAL ELEMENTS OF MATRIX:"+sum);
}
}
Question
PROGRAM TO FIND SUM OF BOUNDARY ELEMENTS OF MATRIX
MATRIX =
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class sumOfBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,sum=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
sum=sum+a[i][j];
}
}
}
System.out.println("Sum of boundary elements of Matrix:"+sum);
}
}
Question
PROGRAM TO FIND SUM OF NON-BOUNDARY ELEMENTS OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class SumNonBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,sum=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
//printing the matrix
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
//calculating sum
System.out.println("Matrix:");
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
sum=sum+a[i][j];
}
}
MATRIX =
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class SumOfDiagonalsOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0,sum=0;
int a[][];
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
if(i==j || (i+j==size-1))
{
sum=sum+a[i][j];
}
System.out.println("SUM OF DIAGONALS OF MATRIX:"+sum);
}
}
Question
PROGRAM TO PRINT BOUNDARY ELEMENTS OF MATRIX
MATRIX =
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class BoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
System.out.print(a[i][j]+" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Question
PROGRAM TO PRINT NON-BOUNDARY ELEMENTS OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
5
Answer
import java.util.Scanner;
public class printNonBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
//printing the matrix
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
}
}
Question
PROGRAM TO LARGEST ELEMENT OF MATRIX
MATRIX:
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class LargestElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,max=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
max=a[0][0];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}
System.out.println("LARGEST ELEMENT OF MATRIX:"+max);
}
}
Question
PROGRAM TO LARGEST ELEMENT IN BOUNDARY ELEMENTS OF MATRIX
MATRIX =
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class largestBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,max=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
max=a[0][0];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
if(a[i][j]> max)
{
max=a[i][j];
}
}
}
}
System.out.println("LARGEST BOUNDARY ELEMENT IN MATRIX:"+max);
}
}
Question
PROGRAM TO FIND SMALLEST ELEMENT IN DIAGONALS OF MATRIX
MATRIX =
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SMALLEST ELEMENT IN DIAGONALS OF MATRIX:1
Answer
import java.util.Scanner;
public class LargestElementInDiagonalsOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0,max=0;
int a[][];
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
a[i][j]=sc.nextInt();
}
}
max=a[0][0];
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
if(i==j || (i+j==size-1))
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}
}
System.out.println("LARGEST ELEMENT IN DIAGONALS OF MATRIX:"+max);
}
}
Question
PROGRAM TO FIND LARGEST ELEMENT IN DIAGONALS OF MATRIX
MATRIX:
[9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class LargestElementInDiagonalsOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0,max=0;
int a[][];
a=new int[size][size];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
a[i][j]=sc.nextInt();
}
}
max=a[0][0];
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
if(i==j || (i+j==size-1))
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}
}
System.out.println("LARGEST ELEMENT IN DIAGONALS OF MATRIX:"+max);
}
}
Question
PROGRAM TO FIND SMALLEST ELEMENT OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
import java.util.Scanner;
public class SmallestElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,min=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
min=a[0][0];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(a[i][j]< min)
{
min=a[i][j];
}
}
}
System.out.println("SMALLEST ELEMENTS OF MATRIX:"+min);
}
}
Question
PROGRAM TO LARGEST ELEMENT IN BOUNDARY ELEMENTS OF MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SMALLEST BOUNDARY ELEMENT IN MATRIX:1
Answer
import java.util.Scanner;
public class SmallestBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,min=0;
int a[][];
a=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
min=a[0][0];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
if(a[i][j]< min)
{
min=a[i][j];
}
}
}
}
System.out.println("SMALLEST BOUNDARY ELEMENT IN MATRIX:"+min);
}
}
Question
PROGRAM SORT MATRIX IN DESCENDING ORDER.
MATRIX = [5, 2, 7]
[9, 6, 4]
[3, 8, 1]
SORTED MATRIX
9 8 7
6 5 4
3 2 1
Answer
import java.util.Scanner;
public class SortMatrixInDescendingOrder
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,l=0,temp=0;
int arr[][];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
}
Question
PROGRAM TO COLUMN-WISE SORTING MATRIX IN ASCENDING ORDER
MATRIX
[3, 20, 1]
[13, 5, 4]
[9, 19, 7]
SORTED MATRIX
3 5 1
9 19 4
13 20 7
Answer
import java.util.Scanner;
public class columnSortingInAscendingOrder
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,temp=0;
int arr[][];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SORTED MATRIX
1 2 3
4 6 5
7 8 9
Answer
import java.util.Scanner;
public class BoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,k=0,temp=0,count=0;
int arr[][],b[];
if(i==0||i==(row-1)||j==0||j==(col-1))
{
count++;
}
}
}
b=new int[count];
System.out.println("ORIGINAL MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0 ||j==0||i==(row-1)||j==(col-1))
{
b[k]=arr[i][j];
k++;
}
}
}
System.out.println("SORTED MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Question
PROGRAM TO SORT NON-BOUNDARY ELEMENTS OF THE MATRIX IN ASCENDING ORDER.
MATRIX = [5, 2, 9, 8, 7]
[9, 6, 4, 2, 3]
[3, 8, 1, 3, 9]
[1, 2, 3, 4, 5]
SORTED MATRIX
[5, 2, 9, 8, 7]
[9, 1, 2, 3, 3]
[3, 4, 6, 8, 9]
[1, 2, 3, 4, 5]
Answer
import java.util.Scanner;
public class SortNonBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,k=0,temp=0,count=0;
int arr[][],b[];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
/*counting the number of non-boundary elements*/
count++;
}
}
b=new int[count];
System.out.println("ORIGINAL MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
k=0;
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
b[k]=arr[i][j];
k++;
}
}
k=0;
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
arr[i][j]=b[k];
k++;
}
}
System.out.println("SORTED MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Question
PROGRAM TO SORT BOUNDARY ELEMENTS IN DESCENDING ORDER IN A MATRIX
MATRIX = [9, 8, 7]
[6, 5, 4]
[3, 2, 1]
SORTED MATRIX
9 8 7
5 6 4
3 2 1
Answer
import java.util.Scanner;
public class SortNonBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,k=0,temp=0,count=0;
int arr[][],b[];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
/*counting the number of non-boundary elements*/
count++;
}
}
b=new int[count];
System.out.println("ORIGINAL MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
k=0;
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
b[k]=arr[i][j];
k++;
}
}
k=0;
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
arr[i][j]=b[k];
k++;
}
}
System.out.println("SORTED MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Question
PROGRAM TO SORT NON-BOUNDARY ELEMENTS OF MATRIX IN DESCENDING ORDER .
MATRIX = [5, 2, 9, 8, 7]
[9, 6, 4, 2, 3]
[3, 8, 1, 3, 9]
[1, 2, 3, 4, 5]
SORTED MATRIX
[5, 2, 9, 8, 7]
[9, 8, 6, 4, 3]
[3, 3, 2, 1, 9]
[1, 2, 3, 4, 5]
Answer
import java.util.Scanner;
public class SortNonBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,k=0,temp=0,count=0;
int arr[][],b[];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
/*counting the number of non-boundary elements*/
if(i!=0&&i!=(row-1)&&j!=0&&j!=(col-1))
{
count++;
}
}
}
b=new int[count];
System.out.println("ORIGINAL MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i!=0 &&j!=0&&i!=(row-1)&&j!=(col-1))
{
b[k]=arr[i][j];
k++;
}
}
}
k=0;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i!=0 &&j!=0&&i!=(row-1)&&j!=(col-1))
{
arr[i][j]=b[k];
k++;
}
}
}
System.out.println("SORTED MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Question
PROGRAM TO PRINT MATRIX IN SPIRAL ORDER
MATRIX
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
SPIRAL
123698745
Answer
import java.util.Scanner;
public class PrintMatrixInSpiralForm
{
left=0;
right=col-1;
top=0;
bottom=row-1;
System.out.println("SPIRAL");
/*Checking count < size so that loop stops if we have printed all the digits in array*/
while(count< size)
{
/* This for loop prints from left to right*/
for (i = left; i <= right; i++)
{
/*we are checking count < size inside loop to stop printing if we have printed all
the digits*/
if(count< size)
{
System.out.print(arr[top][i] + " ");
count++;
}
}
top++;
/* This for loop prints from up to down*/
count++;
}
}
right--;
/* This for loop prints from right to left*/
for (i = right; i >=left; i--)
{
/*we are checking count < size inside loop to stop printing if we have printed all
the digits*/
if(count< size)
{
System.out.print(arr[bottom][i] + " ");
count++;
}
}
bottom--;
/* This for loop prints from down to up*/
}
}
}
Question
PROGRAM TO PRINT MATRIX IN ANTICLOCKWISE SPIRAL ORDER
MATRIX
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
SPIRAL
147896321
Answer
import java.util.Scanner;
public class PrintMatrixInSpiralForm
{
left=0;
right=col-1;
top=0;
bottom=row-1;
System.out.println("SPIRAL Anticlockwise");
/*Checking count < size so that loop stops if we have printed all the digits in array*/
while(count< size)
{
/* This for loop prints from top to bottom*/
count++;
}
}
left++;
/* This for loop prints from left to right*/
right--;
/* This for loop prints from right to left*/
}
}
}
Question
PROGRAM TO ROW-WISE SORTING MATRIX IN ASCENDING ORDER
MATRIX
[3, 20, 1]
[13, 5, 4]
[9, 19, 7]
SORTED MATRIX
1 3 20
4 5 13
7 9 19
Answer
import java.util.Scanner;
public class rowSortingInAscendingOrder
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,temp=0;
int arr[][];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
}
Question
PROGRAM TO COLUMN-WISE SORTING MATRIX IN DESCENDING ORDER
MATRIX
[3, 20, 1]
[13, 5, 4]
[9, 19, 7]
SORTED MATRIX
13 20 7
9 19 4
3 5 1
Answer
import java.util.Scanner;
public class columnSortingInDescendingOrder
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,temp=0;
int arr[][];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
}
Question
PROGRAM TO ROW WISE SORTING MATRIX IN DESCENDING ORDER
MATRIX
[3, 20, 1]
[13, 5, 4]
[9, 19, 7]
SORTED MATRIX
20 3 1
13 5 4
19 9 7
Answer
import java.util.Scanner;
public class rowSortingInDescendingOrder
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,temp=0;
int arr[][];
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
}
Question
PROGRAM TO SEARCH ELEMENT IN THE GIVEN MATRIX
ENTER THE ROW
3
ENTER THE COLUMN
3
ENTER THE ELEMENTS IN ARRAY
1
2
3
4
5
6
1
7
8
ENTER THE ELEMENT TO BE SEARCHED
1
SEARCHED ELEMENT FOUND ROW:1 COLUMN1
SEARCHED ELEMENT FOUND ROW:3 COLUMN1
Answer
import java.util.Scanner;
public class SearchMatrix
{
public static void main()
{
int arr[][];
int i=0,j=0, row=0,col=0,SearchedElement=0,count=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE ROW");
row=sc.nextInt();
System.out.println("ENTER THE COLUMN");
col=sc.nextInt();
arr=new int[row][col];
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(arr[i][j]==SearchedElement)
{
System.out.println("SEARCHED ELEMENT FOUND row:"+(i+1)+" column"+(j+1));
count++;
}
}
}
if(count==0)
{
System.out.println("ELEMENT NOT FOUND");
}
}
}
Question
SHIFT ROWS OF MATRIX IN CYCLIC ORDER THAT IS, SWAP 1ST ROW WITH 2ND ROW, 2ND ROW
WITH 3RD AND LAST ROW WITH 1ST. DISPLAY THE ORIGINAL ARRAY AND ARRAY AFTER
SWAPPING.
ENTER NUMBER OF ROWS:
4
ENTER NUMBER OF COLUMNS:
4
ENTER ELEMENTS IN ARRAY:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ORIGINAL ARRAY:
1234
5678
9 10 11 12
13 14 15 16
ARRAY AFTER CYCLING ROW :
13 14 15 16
1234
5678
9 10 11 12
Answer
import java.util.Scanner;
public class CyclicMatrix
{
public static void main()
{
int[ ][ ] arr,newarr;
int row,col;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of rows:");
row=sc.nextInt();
System.out.println("Enter Number of columns:");
col=sc.nextInt();
arr=new int[row][col];
newarr=new int[row][col];
System.out.println("Enter Elements in Array:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Original Array:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if(i==0)
{
newarr[i][j]=arr[row-1][j];
}
else
{
newarr[i][j]=arr[i-1][j];
}
}
}
import java.util.Scanner;
public class SwapFirstAndLastRow
{
public static void main()
{
int[ ][ ] arr;
int row,col;
int i,j,k,temp=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of rows:");
row=sc.nextInt();
System.out.println("Enter Number of columns:");
col=sc.nextInt();
arr=new int[row][col];
System.out.println("Enter Elements in Array:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Original Array:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
k=row-1;
i=0;
for(j=0;j< col;j++)
{
temp=arr[i][j];
arr[i][j]=arr[k][j];
arr[k][j]=temp;
}
System.out.println("Array after swapping first and last row:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Question
PRINT ELEMENTS BELOW THE LEFT DIAGONAL OF THE INTEGER MATRIX.
ENTER SIZE OF ARRAY:
4
ENTER ELEMENTS IN ARRAY:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ORIGINAL MATRIX:
1234
5678
9 10 11 12
13 14 15 16
ELEMENTS BELOW THE LEFT DIAGONAL OF MATRIX:
5
9 10
13 14 15
Answer
import java.util.Scanner;
public class ElementsBelowLeftDiagonalMatrix
{
public static void main()
{
int[ ][ ] arr;
int m;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array:");
m=sc.nextInt();
arr=new int[m][m];
System.out.println("Enter Elements in Array:");
for(i=0;i< m;i++)
{
for(j=0;j< m;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix:");
for(i=0;i< m;i++)
{
for(j=0;j< m;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
System.out.println();
}
}
}
Question
PRINT ELEMENTS BELOW THE RIGHT DIAGONAL OF THE INTEGER MATRIX.
8
11 12
14 15 16
Answer
import java.util.Scanner;
public class BelowRightDiagonal
{
public static void main()
{
int[ ][ ] arr;
int m;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of Matrix:");
m=sc.nextInt();
arr=new int[m][m];
System.out.println("Enter Elements in Matrix:");
for(i=0;i< m;i++)
{
for(j=0;j< m;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix:");
for(i=0;i< m;i++)
{
for(j=0;j< m;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
System.out.println();
}
}
}
Question
PRINT ELEMENTS ABOVE THE RIGHT DIAGONAL OF THE INTEGER MATRIX.
import java.util.Scanner;
public class AboveRightDiagonal
{
public static void main()
{
int[][] arr;
int m;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of Matrix:");
m=sc.nextInt();
arr=new int[m][m];
System.out.println("Enter Elements in Matrix:");
for(i=0;i< m;i++)
{
for(j=0;j< m;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix:");
for(i=0;i< m;i++)
{
for(j=0;j< m;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}
}
Question
PRINT PRODUCT OF ELEMENTS OF EACH ROW AND COLUMN IN INTEGER MATRIX.
import java.util.Scanner;
public class ProductOfEachRowAndColumnOfSquareMatrix
{
public static void main()
{
int[ ][ ] arr;
int size;
long product;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of integer Matrix:");
size=sc.nextInt();
arr=new int[size][size];
}
for(j=0;j< size;j++)
{
product=1;
for(i=0;i< size;i++)
{
product =product*arr[i][j];
}
System.out.println("The product of elements of Column "+(j+1)+" is="+product);
}
}
Question
PRINT LARGEST ELEMENTS OF EACH ROW OF INTEGER MATRIX.
ENTER SIZE OF INTEGER MATRIX:
4
ENTER ELEMENTS IN MATRIX:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ELEMENTS IN MATRIX:
1234
5678
9 10 11 12
13 14 15 16
THE LARGEST VALUE OF ROW 1 IS=4
THE LARGEST VALUE OF ROW 2 IS=8
THE LARGEST VALUE OF ROW 3 IS=12
THE LARGEST VALUE OF ROW 4 IS=16
Answer
import java.util.Scanner;
public class LargestNumberInEachRow
{
public static void main()
{
int[ ][ ] arr;
int size,max=0;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of integer matrix:");
size=sc.nextInt();
arr=new int[size][size];
}
}
}
Question
PRINT LARGEST ELEMENTS OF EACH COLUMN OF INTEGER MATRIX.
import java.util.Scanner;
public class LargestNumberInEachColumn
{
public static void main()
{
int[ ][ ] arr;
int size,max=0;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of integer matrix:");
size=sc.nextInt();
arr=new int[size][size];
System.out.println("Enter Elements in matrix:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Elements in matrix:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(j=0;j< size;j++)
{
max=arr[0][j];
for(i=0;i< size;i++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
}
}
System.out.println("The Highest value of Column "+(j+1)+" is="+max);
}
}
}