SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Scalar Matrix Program in Java and Python

14 February 2015

Scalar Matrix program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Algorithm:

Step 1: Start.

Step 2: Accept the order of the square matrix.

Step 3: Input all elements of the matrix.

Step 4: Display the matrix.

Step 5: Store the first diagonal element.

Step 6: Check that every non-diagonal element is zero.

Step 7: Check that every diagonal element is equal to the first diagonal element and is not zero.

Step 8: If both conditions are satisfied, display that the matrix is scalar; otherwise, display that it is not scalar.

Step 9: Store the first diagonal element as the reference scalar value.

Step 10: For each cell, use i == j to decide whether to compare with the scalar value or check for zero.

Step 11: Initialize the scalar reference variable with A[0][0].

Step 12: Use a nested row-column loop to compare every matrix position with the required scalar-matrix condition.

Step 13: Update the flag when a diagonal position differs from the scalar reference or a non-diagonal position differs from zero.

Step 14: Stop.

Explanation:

A scalar matrix is a diagonal matrix in which all diagonal elements are equal and all non-diagonal elements are zero.

The program first stores the first diagonal element as the value that every other diagonal element must match. This avoids comparing diagonal elements with each other repeatedly.

For positions where row and column indexes are equal, the program checks whether the element is equal to the stored diagonal value. For all other positions, it checks whether the element is zero.

A flag is changed if either condition fails. At the end, the flag tells whether the matrix satisfies both scalar matrix requirements.

The first diagonal element acts as the reference value. Every later diagonal position is compared with it, while all positions where i != j are checked for zero.

A scalar matrix is a diagonal matrix in which all main diagonal elements are equal. This means two conditions must be checked together. First, every non-diagonal element must be zero. Second, all positions where row and column indexes are equal must contain the same value. The program usually stores the first diagonal value and compares every later diagonal value with it. If either an off-diagonal element is non-zero or a diagonal element differs, the matrix is not scalar.

Java Program:

Java
/**
* The class ScalarMatrix inputs a Matrix and checks whether it is a scalar matrix or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class ScalarMatrix
{
    public static void main(String args[])throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the size of the matrix : ");
        int m=sc.nextInt();
        int A[][]=new int[m][m];

        /* Inputting the matrix */
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                System.out.print("Enter an element : ");
                A[i][j]=sc.nextInt();
            }
        }

        /* Printing the matrix */
        System.out.println("*************************");
        System.out.println("The Matrix is : ");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                System.out.print(A[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println("*************************");

        int p = 0, q = 0, x = A[0][0]; // 'x' is storing the 1st main diagonal element

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                /* Checking that the matrix is diagonal or not */
                if(i!=j && A[i][j]!=0) // All non-diagonal elements must be zero
                {
                    p=1;
                    break;
                }
                /* Checking the matrix for scalarity */
                // All main diagonal elements must be equal to 'x' and non-zero
                if(i==j && (A[i][j]==0 || A[i][j]!=x))
                {
                    q=1;
                    break;
                }
            }
        }

        if(p==0 && q==0)
        System.out.println("The matrix is scalar");
        else
        System.out.println("The matrix is not scalar");
    }
}

Equivalent Python Program:

Python
# Read the matrix or array size and store the values for indexed processing.
# Nested loops are used because each row/column or array position must be checked.
# Print the processed array or matrix in the required output format.

m = int(input("Enter the size of the matrix: "))
A = []

for i in range(m):
    row = []
    for j in range(m):
        row.append(int(input("Enter an element: ")))
    A.append(row)

print("The Matrix is:")
for i in range(m):
    for j in range(m):
        print(A[i][j], end="	")
    print()

p = 0
q = 0
x = A[0][0]

for i in range(m):
    for j in range(m):
        if i != j and A[i][j] != 0:
            p = 1
        if i == j and (A[i][j] == 0 or A[i][j] != x):
            q = 1

if p == 0 and q == 0:
    print("The matrix is Scalar")
else:
    print("The matrix is not Scalar")

Output:

INPUT: Enter the size of the matrix: 4 5 0 0 0 0 5 0 0 0 0 5 0 0 0 0 5 OUTPUT: The Matrix is: 5 0 0 0 0 5 0 0 0 0 5 0 0 0 0 5 The matrix is Scalar

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →