Tuesday 12 March 2019

Find x position in Matrix (JAVA)

 Declare, Instantiate, initialize and print a 2Dimensional array of numbers as follows.                                   

 Given a matrix and a number x, find the position of x in the matrix if it is present in it else print “Not Found”.  



import java.util.Scanner;
public class ZamaCodes
{
public static void main(String []args)
{
int arr[][]=new int[4][];
arr[0]=new int[1];
arr[1]=new int[2];
arr[2]=new int[3];
arr[3]=new int[4];

Scanner s=new Scanner(System.in);
System.out.println("Enter the numbers");
for(int i=0;i<arr.length;i++)
for(int k=0;k<arr[i].length;k++)
{

arr[i][k]=s.nextInt();

}

System.out.println("Printing");
for(int v[] : arr)
for(int x : v)
{
System.out.println(x);
}

System.out.println("Enter the number you want to search in 2d array:");
int x;
x=s.nextInt();

for(int i=0;i<arr.length;i++)
for(int k=0;k<arr[i].length;k++)
if(x==arr[i][k])
{
System.out.println("Value found at index"+"ROW="+i+"Colum="+k);
}
else
{
System.out.println("Not Found");
}
}
}

0 comments:

Post a Comment