Diagonal matrix in C
In this post, you will learn how to write a C program to check weather the matrix is diagonal or not.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are used substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop, which uses a matrix to process linear transformations to render images.
A square matrix in which every element except the principal diagonal elements is zero is called a diagonal matrix. Diagonal matrices occur in many areas of linear algebra.
C program to check whether a matrix is diagonal or not
Here is the source code of the C Program to check whether a matrix is diagonal or not.
#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 user input of the matrix
int i,j;
printf("Enter the Matrix elements :\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.");
}
Output 1:
Enter the row size of the Matrix : 3
Enter the columns size of the Matrix : 3
Enter the Matrix elements :
1 0 0
0 4 0
0 0 3
Given Matrix is a diagonal Matrix.
Output 2
Enter the row size of the Matrix : 2
Enter the columns size of the Matrix : 2
Enter the Matrix elements :
3 0
0 5
Given Matrix is a diagonal Matrix.
Output 3
Enter the row size of the Matrix : 2
Enter the columns size of the Matrix : 2
Enter the Matrix elements :
4 5
2 5
Given Matrix is not a diagonal Matrix.
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative