Problem Statement:- Program to find whether the given is the matrix is diagonal or not.
Definition of Diagonal Matrix:- a square matrix that consists of all zeros off the main diagonal is called a diagonal matrix.
Example:-
Sample Input/Output:-
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 matrix is diagonal 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 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(point==1) printf("Given Matrix is not a diagonal Matrix."); else printf("Given Matrix is a diagonal 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 6 Given Matrix is a diagonal Matrix.
Program in C++
Here is the source code of the C++ Program to check whether a matrix is diagonal 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 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(point==1) cout<<"Given Matrix is not a diagonal Matrix."; else cout<<"Given Matrix is a diagonal 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 0 0 0 0 6 7 0 Given Matrix is not a diagonal Matrix.
Program in Java
Here is the source code of the Java Program to check whether a matrix is diagonal or not.
Code:
import java.util.Scanner; public class CheckDiagonalMatrix { 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 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(point==1) System.out.print("Given Matrix is not a diagonal Matrix."); else System.out.print("Given Matrix is a diagonal 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: 7 0 0 0 7 0 0 0 9 Given Matrix is a diagonal Matrix.Program in Python
Here is the source code of the Python Program to find whether the given is the matrix is diagonal 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 elements are 0 or not 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]!=0: point=1 break if point==1: print("Given Matrix is not a diagonal Matrix.") else: print("Given Matrix is a diagonal Matrix.")
Input/Output:
Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 5 6 0 2 0 Given Matrix is not a diagonal 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