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
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
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];
System.out.println("ENTER THE ELEMENTS IN ARRAY");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("ENTER THE ELEMENT TO BE SEARCHED");
SearchedElement=sc.nextInt();
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");
}
}
}