w3resource
Java Programming Exercies

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?



New Content: Composer: Dependency manager for PHP, R Programming