## Introduction

Programming, like math, is a tremendously enjoyable subject to study. Today, we'll talk about matrix.

A **matrix **is a set of integers organized in columns and rows. A matrix's elements must be contained in brackets or parentheses.

It is a set of numbers arranged in a rectangular or array-like layout in its most basic form. An image, a network, or even a non-physical structure could be used.

We will discuss the two matrix types, **Diagonal **and **Scalar **Matrix, and their implementation in a different language with examples.

Must Read, __Array Implementation of Queue__ and __Rabin Karp Algorithm__

## Diagonal Matrix

A square matrix is a diagonal matrix with all of its principal diagonal members equal to zero. If all of the members of a square matrix except the major diagonal are zero, it is said to be a diagonal matrix.

### Algorithm

- Begin looping through the matrix's elements.
- Check the following for each element:
- It's a diagonal element if the row number equals the column number. Check to see if the value isn't zero.
- If the row number does not equal the column number, it is not a diagonal element. Verify that the value is zero.
- The matrix is not a diagonal matrix if any preceding requirements are untrue and the program returns false.
- Otherwise, true is returned, indicating that the matrix is diagonal.

### Examples:

```
Input :
Matrix[4][0] = {{2, 0, 0, 0},
{0, 3, 0, 0},
{0, 0, 4, 0},
{0, 0, 0, 5}}
Output : Yes
Mat[4][0] = {{4, 3, 2, 1},
{0, 5, 0, 0},
{0, 0, 6, 0},
{0, 0, 0, 7}}
Output : No
```

### Program in Java

The source code for the Java program that checks whether a matrix is diagonal is here.

**Code**:

```
import java.util.Scanner;
class Diagonal_Matrix {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int r,c; //Get size of matrix
r=sc.nextInt();
c=sc.nextInt();
int matrix[][] =new int[r][c];
//Taking input of the matrix
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
matrix[i][j]=sc.nextInt();
}
}
//Ensure that none of the Diagonal elements are 0 or not.
int p=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i!=j && matrix[i][j]!=0)
{
p=1;
break;
}
}
}
if(p==1)
System.out.print("The given matrix isn't a diagonal one.");
else
System.out.print("The given matrix is a diagonal one.");
}
}
```

### Input

```
3 3
1 0 0
0 2 0
0 0 3
```

### Output

`The given matrix is a diagonal one.`

### Program in C++

Here is the source code for a C++ application that tests if a matrix is diagonal.

**Code:**

```
#include<iostream>
using namespace std;
int main()
{
int r,c;
//Get size of matrix
cin>>r;
cin>>c;
int matrix[r][c];
//Taking input of the matrix
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>matrix[i][j];
}
}
//Ensure that none of the Diagonal elements are 0 or not.
int p=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i!=j && matrix[i][j]!=0)
{
p=1;
break;
}
}
}
if(p==1)
cout<<"The given matrix isn't a diagonal one.";
else
cout<<"The given matrix is a diagonal one.";
}
```

### Input

```
3 3
2 0 0
0 0 0
3 4 0
```

### Output

`The given matrix isn't a diagonal one`

### Program in Python

The source code for the python program that checks whether a matrix is diagonal is here.

**Code**:

```
# Get size of matrix
r=int(input(""))
c=int(input(""))
matrix=[]
# Taking input of the 1st matrix
for i in range(r):
matrix.append([int(j) for j in input().split()])
# check except Diagonal elements are 0 or not
p=0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
# check for diagonals element
if i!=j and matrix[i][j]!=0:
p=1
break
if p==1:
print("Given Matrix is not a diagonal Matrix.")
else:
print("Given Matrix is a diagonal Matrix.")
```

### Input

```
3
3
7 0 0
0 8 0
0 0 9
```

### Output

`Given Matrix is a diagonal Matrix.`