0% found this document useful (0 votes)
116 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.

Uploaded by

Richin Varghese
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 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.

Uploaded by

Richin Varghese
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
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];
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%4d",A[i][j]);
printf("%8d",R[i]);
printf("\n");
}
printf("\n");
for(j=0;j<n;j++)
printf("%4d",C[j]);

getch();
}



2 . /* Write a C Program to check if a given matrix is an
identity matrix */
#include <stdio.h>

void main()
{
int A[10][10];
int i, j, R, C, flag =1;

printf("Enter the order of the matrix A\n");
scanf("%d %d", &R, &C);

printf("Enter the elements of matrix A\n");
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
scanf("%d",&A[i][j]);
}
}
printf("MATRIX A is\n");
for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

/* Check for unit (or identity) matrix */

for(i=0; i<R; i++)
{
for(j=0; j<C; j++)
{
if(A[i][j] != 1 && A[j][i] !=0)
{
flag = 0;
break;
}
}
}

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);

return 0;
}

You might also like