This C program contains 4 code snippets that demonstrate operations on matrices:
1. Calculates the sum of rows and columns of a 2D matrix entered by the user.
2. Checks if a matrix entered by the user is an identity matrix by comparing elements.
3. Checks if a matrix is symmetric by comparing the matrix to its transpose.
4. Calculates the sum of the diagonal elements of a matrix.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
98 views
Program For Sum of Row and Column of Matrix
This C program contains 4 code snippets that demonstrate operations on matrices:
1. Calculates the sum of rows and columns of a 2D matrix entered by the user.
2. Checks if a matrix entered by the user is an identity matrix by comparing elements.
3. Checks if a matrix is symmetric by comparing the matrix to its transpose.
4. Calculates the sum of the diagonal elements of a matrix.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
1.
/* PROGRAM FOR SUM OF ROW AND COLUMN of
MATRIX */ #include<stdio.h> #include<conio.h> main() { int i,j,k,A[10][10],R[10],C[10],m,n; clrscr(); printf(" ENTER THE SIZE OF A MATRIX \n"); scanf("%d%d",&m,&n); printf("ENTER THE ELEMENT OF A MATRIX \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&A[i][j]); /* Sum of rows */ for(i=0;i<m;i++) { R[i]=0; for(j=0;j<n;j++) R[i]=R[i]+A[i][j]; }
/* Sum of Column */ for(i=0;i<n;i++) { C[i]=0; for(j=0;j<m;j++) C[i]=C[i]+A[j][i]; }
if(flag == 1 ) printf("It is identity matrix\n"); else printf("It is not a identity matrix\n"); }
3. c program to check symmetric matrix
#include<stdio.h> #include<conio.h>
Void main() { int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d",&m,&n); printf("Enter the elements of matrix\n");
for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&matrix[c][d]);
for( c = 0 ; c < m ; c++ ) { for( d = 0 ; d < n ; d++ ) { transpose[d][c] = matrix[c][d]; } }
if ( m == n ) /* check if order is same */ { for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < m ; d++ ) { if ( matrix[c][d] != transpose[c][d] ) break; } if ( d != m ) break; } if ( c == m ) printf("Symmetric matrix.\n"); } else printf("Not a symmetric matrix.\n");
}
4. Sum of diagonal elements of a matrix in c
#include<stdio.h>
int main(){
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: "); scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: "); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n");
for(i=0;i<m;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d\t",a[i][j]); } } for(i=0;i<m;i++){ for(j=0;j<n;j++){ if(i==j) sum=sum+a[i][j]; } } printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);