Java Program Searching A Number In 2d arrays
JAVA Programming Language Java Arrays

Java Program Searching A Number In 2d arrays


In this article, we will write a Java Program Searching A Number In 2d arrays. First, you have some basic knowledge about the arrays.

Arrays are the non primitive data type used to store the multiple values for a same variable. So, without wasting our time let’s move on to the program.

Java Program Searching A Number In 2d arrays:

In this section, we are going to discuss the whole logic that how will we search a number in the multi dimensional arrays. We are not discussing any basics in this article because we have already discuss the basics of java in our previous programs.

By the use of scanner class we take the values of rows and columns from the user and then declare the 2d array. The size of the array depends on the rows and columns entered by the user both rows and columns multiply. Suppose we enter 3×2 it means that there are total six elements in the array or in a matrix.

Then, we are taking the elements of the array from the user. After taking the values we simply declare a variable ‘x’ and using if statement. The condition is if the arr[i][j] == x means that if the total six elements that we enter is equal to x then print Number is found at i(row) and j(which is column). This is the simplest program to understand the concept of multi dimensional array.

Source Code Of Java Program Searching A Number In 2d arrays;

import java.util.*;
public class Demo {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Rows and Columns Of Array :");
int rows= sc.nextInt();
int col=sc.nextInt();
int array[][]=new int[rows][col];
System.out.println("Enter The Elements Of Array :");
for (int i=0;i<rows;i++){
for (int j=0;j<col;j++){
array[i][j]=sc.nextInt();
}
}
System.out.println("Enter the number you want to search :");
int x=sc.nextInt();
for (int i=0;i<rows;i++){
for (int j=0;j<col;j++){
if (array[i][j]==x){
System.out.println("Number is found at " + i +" row" + j + " column");
}
}
}
}
}

Output:

Java Program Searching A Number In 2d arrays
Java Program Searching A Number In 2d arrays

 

 

 

 

 

 

 

Conclusion:

From the above program, we conclude that how to write a Java Program Searching A Number In 2d arrays and we will show you the output of the program. We hope that your all problems will be clear about this program now. If you want to explore more about Java programming language, please click here.

We are providing you the link of the video in which Java Program Searching A Number In 2d arrays is fully and easisly explained.

Leave a Reply

Your email address will not be published. Required fields are marked *