Program to check diagonal matrix and scalar matrix

Diagonal matrix:

A square matrix is said to be diagonal matrix if the elements of matrix except main diagonal are zero. A square null matrix is also a diagonal matrix whose main diagonal elements are zero.
Examples :

Input : 
Mat[4][0] = {{4, 0, 0, 0},
             {0, 5, 0, 0},
             {0, 0, 2, 0},
         {0, 0, 0, 1}}
Output : Yes

Mat[4][0] = {{6, 10, 12, 0},
             {0, 5, 0, 0},
             {0, 0, 9, 0},
             {0, 0, 0, 1}}
Output : No



CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to check matrix is diagonal matrix or not.
#include <bits/stdc++.h>
#define N 4
using namespace std;
  
// Function to check matrix is diagonal matrix
// or not.
bool isDiagonalMatrix(int mat[N][N])
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
  
            // condition to check other elements 
            // except main diagonal are zero or not.
            if ((i != j) && (mat[i][j] != 0))
                return false;
    return true;
}
  
// Driver function
int main()
{
    int mat[N][N] = { { 4, 0, 0, 0 },
                      { 0, 7, 0, 0 },
                      { 0, 0, 5, 0 },
                      { 0, 0, 0, 1 } };
  
    if (isDiagonalMatrix(mat))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to check matrix is
// diagonal matrix or not.
  
import java.io.*;
  
class GFG {
      
    static int N = 4;
      
    // Function to check matrix
    // is diagonal matrix
    // or not.
    static boolean isDiagonalMatrix(int mat[][])
    {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
       
                // condition to check
                // other elements 
                // except main diagonal 
                // are zero or not.
                if ((i != j) &&
                   (mat[i][j] != 0))
                    return false;
  
        return true;
    }
       
    // Driver function
    public static void main(String args[])
    {
        int mat[][] = { { 4, 0, 0, 0 },
                          { 0, 7, 0, 0 },
                          { 0, 0, 5, 0 },
                          { 0, 0, 0, 1 } };
       
        if (isDiagonalMatrix(mat))
            System.out.println("Yes");
        else
            System.out.println("No" );
    }
}
  
// This code is contributed by
// Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to check if matrix
# is diagonal matrix or not.
  
N = 4
  
# Function to check matrix
# is diagonal matrix
# or not.   
def isDiagonalMatrix(mat) :
    for i in range(0, N):
        for j in range(0, N) :
              
            # condition to check
            # other elements 
            # except main diagonal
            # are zero or not.
            if ((i != j) and
             (mat[i][j] != 0)) :
                return False
  
    return True
  
  
# Driver function
mat = [[ 4, 0, 0, 0 ],
       [ 0, 7, 0, 0 ],
       [ 0, 0, 5, 0 ],
       [ 0, 0, 0, 1 ]]
  
if (isDiagonalMatrix(mat)) :
    print("Yes")
else :
    print("No")
      
      
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to check matrix is
// diagonal matrix or not.
using System;
  
class GFG {
      
    static int N = 4;
      
    // Function to check matrix
    // is diagonal matrix
    // or not.
    static bool isDiagonalMatrix(int [,]mat)
    {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
      
                // condition to check
                // other elements 
                // except main diagonal 
                // are zero or not.
                if ((i != j) &&    (mat[i,j] != 0))
                    return false;
  
        return true;
    }
      
    // Driver function
    public static void Main()
    {
        int [,]mat = { { 4, 0, 0, 0 },
                        { 0, 7, 0, 0 },
                        { 0, 0, 5, 0 },
                        { 0, 0, 0, 1 } };
      
        if (isDiagonalMatrix(mat))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No" );
    }
}
  
// This code is contributed by
// vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to check matrix is
// diagonal matrix or not.
  
$N= 4;
  
// Function to check matrix is
// diagonal matrix or not.
function isDiagonalMatrix($mat)
{
    global $N;
    for ($i = 0; $i < $N; $i++)
        for ($j = 0; $j < $N; $j++)
  
            // condition to check
            // other elements 
            // except main diagonal
            // are zero or not.
            if (($i != $j) && 
                ($mat[$i][$j] != 0))
                return false;
    return true;
}
  
    // Driver Code
    $mat = array(array(4, 0, 0, 0),
                 array(0, 7, 0, 0),
                 array(0, 0, 5, 0),
                 array(0, 0, 0, 1));
  
    if (isDiagonalMatrix($mat))
        echo "Yes" ,"\n";
    else
        echo "No","\n";
  
// This code is contributed by jit_t
?>

chevron_right



Output:

Yes

Scalar matrix:

A square matrix is said to be scalar matrix if all the main diagonal elements are equal and other elements except main diagonal are zero. Scalar matrix can also be written in form of n * I, where n is any real number and I is the identity matrix.
Examples:

Input :
Mat[4][4] = {{4, 0, 0, 0},
             {0, 4, 0, 0},
             {0, 0, 4, 0},
             {0, 0, 0, 4}} 
Output : Yes

Mat[4][4] = {{4, 0, 0, 0},
             {0, 4, 0, 0},
             {0, 0, 1, 0},
             {0, 0, 0, 4}} 
Output : No

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to check matrix
// is scalar matrix or not.
import java.io.*;
  
class GFG {
      
    static int N = 4;
    // Function to check matrix
    // is scalar matrix or not.
    static boolean isScalarMatrix(int mat[][])
    {
        // 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
    public static void main(String args[])
    {
        int mat[ ][ ] = { { 2, 0, 0, 0 },
                          { 0, 2, 0, 0 },
                          { 0, 0, 2, 0 },
                          { 0, 0, 0, 2 } };
        // Function call
        if (isScalarMatrix(mat))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed
// by Nikita tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to check matrix
# is scalar matrix or not.
  
  
N = 4
  
# Function to check matrix is
# scalar matrix or not.
def isScalarMatrix(mat) :
      
    # Check all elements 
    # except main diagonal are
    # zero or not.
    for i in range(0,N) :
        for j in range(0,N) :
            if ((i != j) 
               and (mat[i][j] != 0)) :
                return False
   
    # Check all diagonal
    # elements are same or not.
    for i in range(0,N-1) :
        if (mat[i][i] !=
           mat[i + 1][i + 1]) :
            return False
  
    return True
  
  
# Driver function
mat = [[ 2, 0, 0, 0 ],
       [ 0, 2, 0, 0 ],
       [ 0, 0, 2, 0 ],
       [ 0, 0, 0, 2 ]]
  
# Function call
if (isScalarMatrix(mat)):
    print("Yes")
else :
    print("No")
      
      
# This code is contributed by
# Nikita tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to check matrix
// is scalar matrix or not.
using System;
  
class GFG {
      
    static int N = 4;
      
    // Function to check matrix
    // is scalar matrix or not.
    static bool isScalarMatrix(int [,]mat)
    {
        // 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
    public static void Main()
    {
        int [,]mat = { { 2, 0, 0, 0 },
                        { 0, 2, 0, 0 },
                        { 0, 0, 2, 0 },
                        { 0, 0, 0, 2 } };
        // Function call
        if (isScalarMatrix(mat))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed
// by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Program to check matrix
// is scalar matrix or not.
  
$N = 4;
  
// Function to check matrix 
// is scalar matrix or not.
function isScalarMatrix($mat)
{
    global $N;
    // Check all elements except 
    // main diagonal are zero or not.
    for ($i = 0; $i < $N; $i++)
        for ($j = 0; $j < $N; $j++)
            if (($i != $j) &&
                ($mat[$i][$j] != 0))
                return false;
  
    // Check all diagonal elements 
    // are same or not.
    for ($i = 0; $i < $N - 1; $i++)
        if ($mat[$i][$i] != $mat[$i + 1][$i + 1])
            return false;
    return true;
}
  
// Driver Code
$mat = array(array(2, 0, 0, 0),
             array(0, 2, 0, 0),
             array(0, 0, 2, 0),
             array(0, 0, 0, 2));
               
// Function call
if (isScalarMatrix($mat))
    echo "Yes";
else
    echo "No" ;
  
// This code is contributed by mits
?>

chevron_right



Output :

Yes


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 : jit_t, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


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