2 D Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

//primary diagonal sum

#include <stdio.h>

int main()
{
int r,c,i,j,pdiag_sum = 0;

printf("Enter the number of rows (r): ");


scanf("%d", &r);
printf("Enter the number of columns (c): ");
scanf("%d", &c);

int matrix[r][c];

printf("Enter %d elements of the matrix:\n",r*c);


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("The Matirx are:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

for (i = 0; i < r; i++)


{
for (j = 0; j < c; j++)
{
if(i==j)
{
pdiag_sum=pdiag_sum+matrix[i][j];
}

}
}
printf("The Sum of primary diagonal elements is : %d\n", pdiag_sum);

return 0;
}
// sum of secondary diagonal elements
#include <stdio.h>

int main()
{
int r,c,i,j,Sdiag_sum = 0;

printf("Enter the number of rows (r): ");


scanf("%d", &r);
printf("Enter the number of columns (c): ");
scanf("%d", &c);

int matrix[r][c];

printf("Enter %d elements of the matrix:\n",r*c);


for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("The Matirx are:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

Sdiag_sum = 0;

for (i = 0; i < r && i < c; i++)


{
Sdiag_sum =Sdiag_sum + matrix[i][r-1-i];
}

printf("The Sum of Secondary diagonal elements is : %d\n", Sdiag_sum);

return 0;
}
//symmetric or not
#include <stdio.h>
int main()
{
int n, m, symmetric = 1;

printf("Enter the number of rows (n): ");


scanf("%d", &n);
printf("Enter the number of columns (m): ");
scanf("%d", &m);
if (n != m)
{
printf("The matrix is not symmetric because it's not a square matrix.\n");
return 0;
}
int matrix[n][m];

printf("Enter %d elements of the matrix:\n",n*m);


for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("The given matrix is:\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%5d", matrix[i][j]);
}
printf("\n");
}

// Check if matrix[i][j] == matrix[j][i]


for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (matrix[i][j] != matrix[j][i])
{
symmetric = 0;
break;
}
}
}
// Output the result
if (symmetric==1)
{
printf("The above matrix is symmetric.\n");
}
else
{
printf("The above matrix is not symmetric.\n");
}

return 0;
}
// Addition of two matrix
#include<stdio.h>
int main()
{
int i,j,row1,col1,row2,col2;

printf("Enter the number of rows and columns for A matrix:");


scanf("%d %d", &row1, &col1);
printf("Enter the number of rows and columns for B matrix:");
scanf("%d %d", &row2, &col2);
int a[row1][col1],b[row2][col2],c[row1][col1];

if( (row1!= row2) || (col1!=col2) ) // checking the compatability for addition


{
printf("Error: addition is not possible\n");
}

else
{
printf("Enter %d elements for A \n",row1*col1);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("Enter %d elements for B \n",row2*col2);


for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("The Elements of Matrix A is\n");

for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("%d\t",a[i][j]);

}
printf("\n");
}
printf("The Elements of Matrix B is\n");

for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

for(i=0;i<row1;i++)
{
for(j=0;j<row2;j++)
{
c[i][j]=a[i][j]+b[i][j];

}
}

printf("The sum of matrix A and B is:\n");


for(i=0;i<row1;i++)
{
for(j=0;j<row2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n\n");

}
}
return 0;
}
// write c program to find the multiplication of two matrix
#include <stdio.h>

int main()
{

int row1, col1, row2, col2,i,j,k;

printf("Enter the number of rows and columns for A matrix:");


scanf("%d %d", &row1, &col1);
printf("Enter the number of rows and columns for B matrix:");
scanf("%d %d", &row2, &col2);
int A[row1][col1],B[row2][col2],C[row1][col2];

if (col1 != row2)
{
printf("Error:Multipilication is not possible\n");

}
else
{
printf("Enter %d elements of A matrix: \n",row1*col1);
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{

scanf("%d", &A[i][j]);
}
}

printf("Enter %d elements of B matrix:\n",row2*col2);


for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{

scanf("%d", &B[i][j]);
}
}

// Matrix multiplication
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
C[i][j]=0;
for (k = 0; k < col1; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}

// Display the result matrix


printf("\nThe result of matrix multiplication is:\n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
printf("%d\t", C[i][j]);
}
printf("\n");
}
}
return 0;
}

You might also like