Check whether a given matrix is an identity matrix or not

Problem Statement:- Program to check whether a given matrix is an identity matrix or not.

Definition of Identity Matrix:- a scaler matrix S is said to be an identity matrix if each entry on its diagonal equal to 1. This matrix is denoted by Iₙ.


Example:-

Sample Input/Output:-


Sample Input First:2 2

1 0 0 1

Sample Output First: 

Given Matrix is an identity matrix.

Sample Input Second: 3 3

1 0 3 4 1 6 7 0 1

Sample Output Second: 

Given Matrix is not an identity matrix.


Data requirement:-


  Input Data:- row_size, col_size, matrix[][]


  Output Data:-String output


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to Check whether a given matrix is an identity matrix 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 Diagonal elements are 1 and rest elements are 0 int point=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { //check for diagonals element if(i==j && matrix[i][j]!=1) { point=1; break; } //check for rest elements else if(i!=j && matrix[i][j]!=0) { point=1; break; } } } if(point==1) printf("Given Matrix is not an identity matrix."); else printf("Given Matrix is an identity matrix."); }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 1 0 0 1 Given Matrix is an identity matrix.


Program in C++

Here is the source code of the C++ Program to Check whether a given matrix is an identity matrix 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 Diagonal elements are 1 and rest elements are 0 int point=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { //check for diagonals element if(i==j && matrix[i][j]!=1) { point=1; break; } //check for rest elements else if(i!=j && matrix[i][j]!=0) { point=1; break; } } } if(point==1) cout<<"Given Matrix is not an identity matrix."; else cout<<"Given Matrix is an identity matrix."; }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 1 0 3 4 1 6 7 0 1 Given Matrix is not an identity matrix.


Program in Java

Here is the source code of the Java Program to Check whether a given matrix is an identity matrix or not.


Code:


import java.util.Scanner; public class CheckIdentityMatrix { 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 Diagonal elements are 1 and rest elements are 0 int point=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { //check for diagonals element if(i==j && matrix[i][j]!=1) { point=1; break; } //check for rest elements else if(i!=j && matrix[i][j]!=0) { point=1; break; } } } if(point==1) System.out.print("Given Matrix is not an identity matrix."); else System.out.print("Given Matrix is an identity 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 0 0 0 1 0 0 0 1 Given Matrix is an identity matrix.

Program in Python

Here is the source code of the Python Program to Check whether a given matrix is an identity matrix 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 Diagonal elements are 1 and rest elements are 0 point=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] != 1: point=1 break #check for rest elements elif i!=j and matrix[i][j]!=0: point=1 break if point==1: print("Given Matrix is not an identity matrix.") else: print("Given Matrix is an identity matrix.")


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 1 0 0 1 0 1 Given Matrix is not an identity matrix.



Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments