Java Exercises: Find the all positions of a given number in a given matrix
Java Basic: Exercise-194 with Solution
Write a Java program to find the all positions of a given number in a given matrix. If the number not found print ("Number not found!").
Pictorial Presentation:
Sample Solution:
Java Code:
public class Solution {
public static void main(String[] args) {
int num = 3;
int matrix[][] = {
{2,5,3},
{3,2,1},
{1,3,5}
};
int r = matrix.length;
int c = matrix[0].length - 1;
int m = 0, n = 0;
Boolean flag = false;
while (m < r) {
while (n <= c) {
if (matrix[m][n] == num) {
System.out.print("\n(" + m + "," + n + ")\n");
flag = true;
}
n++;
}
m++;
n = 0;
}
if (flag == false)
System.out.print("Number not found!");
}
}
Sample Output:
(0,2) (1,0) (2,1)
Flowchart:
Java Code Editor:
Company: LinkedIn
Contribute your code and comments through Disqus.
Previous: Write a Java program that accept an integer and find the sum of all the elements from all possible subsets of a set formed by first n natural numbers.
Next: Write a Java program to check if three given side lengths (integers) can make a triangle or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework