Problem Statement:- Program to check whether a matrix is a scalar or not.
Definition of Scalar Matrix:- In a diagonal matrix if all elements are equal the matrix is called a Scalar matrix.
Example:-
Sample Input/Output:-
Data requirement:-
Input Data:- row_size, col_size, matrix[][]
Output Data:-String output
Additional Data:- i, j, point
Program in C
Here is the source code of the C Program to check whether a matrix is a scalar or not.
Code:
#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } //check except Diagonal elements are 0 or not //and check all diagonal elements are same or not int point=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i!=j && matrix[i][j]!=0) { point=1; break; } if(i==j && matrix[i][j]!=matrix[i][j]) { point=1; break; } } } if(point==1) printf("Given Matrix is not a Scaler Matrix."); else printf("Given Matrix is a Scaler Matrix."); }
Input/Output:
Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 5 0 0 5 Given Matrix is a Scaler Matrix.
Program in C++
Here is the source code of the C++ Program to check whether a matrix is a scalar or not.
Code:
#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } //check except Diagonal elements are 0 or not //and check all diagonal elements are same or not int point=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i!=j && matrix[i][j]!=0) { point=1; break; } if(i==j && matrix[i][j]!=matrix[i][j]) { point=1; break; } } } if(point==1) cout<<"Given Matrix is not a Scaler Matrix."; else cout<<"Given Matrix is a Scaler Matrix."; }
Input/Output:
Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 9 0 0 0 9 0 0 0 9 Given Matrix is a Scaler Matrix.
Program in Java
Here is the source code of the Java Program to check whether a matrix is a scalar or not.
Code:
import java.util.Scanner; public class CheckScalerMatrix { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } //check except Diagonal elements are 0 or not //and check all diagonal elements are same or not int point=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(i!=j && matrix[i][j]!=0) { point=1; break; } if(i==j && matrix[i][j]!=matrix[i][j]) { point=1; break; } } } if(point==1) System.out.print("Given Matrix is not a Scaler Matrix."); else System.out.print("Given Matrix is a Scaler Matrix."); cs.close(); } }
Input/Output:
Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 1 2 0 0 1 0 0 0 0 Given Matrix is not a Scaler Matrix.Program in Python
Here is the source code of the Python Program to check whether a matrix is a scalar or not.
Code:
# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) # check except Diagonal all elements are 0 or not # and check all diagonal elements are same or not point=0 for i in range(len(matrix)): for j in range(len(matrix[0])): if i!=j and matrix[i][j]!=0: point=1 break if i==j and matrix[i][j]!=matrix[i][j]: point = 1 break if point==1: print("Given Matrix is not a Scaler Matrix.") else: print("Given Matrix is a Scaler Matrix.")
Input/Output:
Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 4 0 0 4 Given Matrix is a Scaler Matrix.
Most Recommend Questions:-
More Questions:-
Array Programming Questions and Solutions(50)
0 Comments
Please do not Enter any spam link in the comment box