# Program to check if a matrix is Binary matrix or not

Given a matrix, the task is to check if that matrix is a Binary Matrix. A Binary Matrix is a matrix in which all the elements are either 0 or 1. It is also called Logical Matrix, Boolean Matrix, Relation Matrix.

Examples:

```Input:
{{1, 0, 1, 1},
{0, 1, 0, 1}
{1, 1, 1, 0}}
Output: Yes

Input:
{{1, 0, 1, 1},
{1, 2, 0, 1},
{0, 0, 1, 1}}
Output: No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse the matrix and check if every element is either 0 or 1. If there is any element other than 0 and 1, print No else print Yes.

Below is the implementation of above approach:

## C++

 `// C++ code to check if a matrix ` `// is binary matrix or not. ` `#include ` `using` `namespace` `std; ` ` `  `#define M 3 ` `#define N 4 ` ` `  `// function to check if a matrix ` `// is binary matrix or not ` `bool` `isBinaryMatrix(``int` `mat[][N]) ` `{ ` `    ``for` `(``int` `i = 0; i < M; i++) { ` `        ``for` `(``int` `j = 0; j < N; j++) { ` `            ``// Returns false if element is other than 0 or 1. ` `            ``if` `(!(mat[i][j] == 0 || mat[i][j] == 1)) ` `                ``return` `false``; ` `        ``} ` `    ``} ` ` `  `    ``// Returns true if all the elements ` `    ``// are either 0 or 1. ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `mat[M][N] = { { 1, 0, 1, 1 }, ` `                      ``{ 0, 1, 0, 1 }, ` `                      ``{ 1, 1, 1, 0 } }; ` ` `  `    ``if` `(isBinaryMatrix(mat)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// JAVA code to check if a matrix ` `// is binary matrix or not. ` ` `  `import` `java.io.*; ` `class` `GFG { ` `    ``static` `int` `M = ``3``; ` `    ``static` `int` `N = ``4``; ` ` `  `    ``// function to check if a matrix is binary matrix ` `    ``// or not ` `    ``static` `boolean` `isBinaryMatrix(``int` `mat[][]) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < M; i++) { ` `            ``for` `(``int` `j = ``0``; j < N; j++) { ` `                ``// Returns false if element is other than 0 or 1. ` `                ``if` `(!(mat[i][j] == ``0` `|| mat[i][j] == ``1``)) ` `                    ``return` `false``; ` `            ``} ` `        ``} ` ` `  `        ``// Returns true if all the elements ` `        ``// are either 0 or 1. ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `mat[][] = { { ``1``, ``0``, ``1``, ``1` `}, ` `                        ``{ ``0``, ``1``, ``0``, ``1` `}, ` `                        ``{ ``1``, ``1``, ``1``, ``0` `} }; ` ` `  `        ``if` `(isBinaryMatrix(mat)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} `

## Python3

 `# Python3 code to check if a matrix ` `# is binary matrix or not. ` ` `  `M ``=` `3``; ` `N ``=` `4``; ` ` `  `# function to check if a matrix ` `# is binary matrix or not ` `def` `isBinaryMatrix(mat): ` `    ``for` `i ``in` `range``(M): ` `        ``for` `j ``in` `range``(N): ` `            ``# Returns false if element ` `            ``# is other than 0 or 1. ` `            ``if` `((mat[i][j] ``=``=` `0` `or` `mat[i][j] ``=``=` `1``)``=``=``False``): ` `                ``return` `False``; ` ` `  `    ``# Returns true if all the  ` `    ``# elements are either 0 or 1. ` `    ``return` `True``; ` ` `  `# Driver code ` `if` `__name__``=``=``'__main__'``: ` `    ``mat ``=` `[[ ``1``, ``0``, ``1``, ``1` `],[``0``, ``1``, ``0``, ``1` `],[ ``1``, ``1``, ``1``, ``0` `]]; ` ` `  `    ``if` `(isBinaryMatrix(mat)): ` `        ``print``(``"Yes"``); ` `    ``else``: ` `        ``print``(``"No"``); ` ` `  `# This code is contributed by mits `

## C#

 `// C# code to check if a matrix  ` `// is binary matrix or not.  ` ` `  `using` `System; ` `class` `GFG {  ` `    ``static` `int` `M = 3;  ` `    ``static` `int` `N = 4;  ` ` `  `    ``// function to check if a matrix is binary matrix  ` `    ``// or not  ` `    ``static` `bool` `isBinaryMatrix(``int` `[,]mat)  ` `    ``{  ` `        ``for` `(``int` `i = 0; i < M; i++) {  ` `            ``for` `(``int` `j = 0; j < N; j++) {  ` `                ``// Returns false if element is other than 0 or 1.  ` `                ``if` `(!(mat[i,j] == 0 || mat[i,j] == 1))  ` `                    ``return` `false``;  ` `            ``}  ` `        ``}  ` ` `  `        ``// Returns true if all the elements  ` `        ``// are either 0 or 1.  ` `        ``return` `true``;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[,]mat = { { 1, 0, 1, 1 },  ` `                        ``{ 0, 1, 0, 1 },  ` `                        ``{ 1, 1, 1, 0 } };  ` ` `  `        ``if` `(isBinaryMatrix(mat))  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``else` `            ``Console.WriteLine(``"No"``);  ` `    ``}  ` `}  ` `// This code is contributed by anuj_67. `

## PHP

 ` `

Output:

```Yes
```

Time complexity: O( M X N)
Space Complexity: O(1)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : inderDuMCA, Mithun Kumar

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.