Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

Java Basic Exercises: Test whether there are two integers x and y

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:

Flowchart: Java exercises: Find the all positions of a given number in a given matrix

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