// Program to check matrix is scalar matrix or not.
#include <bits/stdc++.h>
#define N 4
using namespace std;
// Function to check matrix is scalar matrix or not.
bool isScalarMatrix(int mat[N][N])
{
// Check all elements except main diagonal are
// zero or not.
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if ((i != j) && (mat[i][j] != 0))
return false;
// Check all diagonal elements are same or not.
for (int i = 0; i < N - 1; i++)
if (mat[i][i] != mat[i + 1][i + 1])
return false;
return true;
}
// Driver function
int main()
{
int mat[N][N] = { { 2, 0, 0, 0 },
{ 0, 2, 0, 0 },
{ 0, 0, 2, 0 },
{ 0, 0, 0, 2 } };
// Function call
if (isScalarMatrix(mat))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: