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
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class LargestElementInDiagonalsOfMatrix
{
public static void main(String[] args)
{
int size=0,i=0,j=0,max=0;
int a[][];
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SIZE OF MATRIX");
size=sc.nextInt();
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);
}
}
Python
import numpy as np
size=int(input("Enter size of Matrix:"))
matrix=np.empty([size,size],dtype=np.int)
# Taking input from user
print("Enter elements in matrix:")
for i in range(0,size):
for j in range(0,size):
print("(",(i+1),",",(j+1),")",":",end="")
matrix[i,j]=int(input())
#printing the matrix
print("Matrix:")
for i in range(0, size):
print(matrix[i])
smallestDiagonalElement=matrix[0,0]
#Finding smallest Diagonal elements
for i in range(0,size):
for j in range(0,size):
if(i==j or (i+j==size-1)):
if(matrix[i,j]< smallestDiagonalElement):
smallestDiagonalElement=matrix[i,j]
print("Smallest Diagonal elements:",smallestDiagonalElement)