0% found this document useful (0 votes)
5 views5 pages

10 C Programs for Matrix Operations

questions

Uploaded by

Nitya Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

10 C Programs for Matrix Operations

questions

Uploaded by

Nitya Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

25/09/2025, 21:26 Selected files

Selected files
10 printable files

q1.c
q2.c
q3.c
q4.c
q5.c
q6.c
q7.c
q8.c
q9.c
q10.c

q1.c

1 /*
2 A matrix is a two-dimensional array of numbers arranged in rows and columns.
3 Write a program to read a 3×3 matrix from the user and display it in matrix form.
4 */
5 #include <stdio.h>
6 int main(){
7 int a[3][3], i, j;
8 for(i=0;i<3;i++)
9 for(j=0;j<3;j++)
10 scanf("%d",&a[i][j]);
11 for(i=0;i<3;i++){
12 for(j=0;j<3;j++)
13 printf("%d ",a[i][j]);
14 printf("\n");
15 }
16 }
17

q2.c

1 /*
2 In a matrix, the row sum is the sum of elements in each row, and the column sum is the sum
3 of elements in each column.
4 Write a program to calculate and print the sum of each row and each column of a 3×3
5 matrix.
6 */
7 #include <stdio.h>
8 int main(){
9 int a[3][3],i,j,s;
10 for(i=0;i<3;i++)
11 for(j=0;j<3;j++)
12 scanf("%d",&a[i][j]);
13 for(i=0;i<3;i++){
14 s=0;
15 for(j=0;j<3;j++) s+=a[i][j];
16 printf("Row %d = %d\n",i+1,s);

localhost:53236/60823f66-851f-4e91-b5e7-e4a560f6f49a/ 1/5
25/09/2025, 21:26 Selected files

17 }
18 for(j=0;j<3;j++){
19 s=0;
20 for(i=0;i<3;i++) s+=a[i][j];
21 printf("Col %d = %d\n",j+1,s);
22 }
23 }
24

q3.c

1 /*
2 The transpose of a matrix is obtained by interchanging its rows and columns.
3 For example, the transpose of
4 1 2 3
5 4 5 6
6 is
7 1 4
8 2 5
9 3 6
10 Write a program to find the transpose of a given matrix
11 */
12 #include <stdio.h>
13 int main(){
14 int a[3][3], i,j;
15 for(i=0;i<3;i++)
16 for(j=0;j<3;j++) scanf("%d",&a[i][j]);
17 for(i=0;i<3;i++){
18 for(j=0;j<3;j++)
19 printf("%d ",a[j][i]);
20 printf("\n");
21 }
22 }
23

q4.c

1 /*
2 The sum of two matrices of the same dimensions is obtained by adding the corresponding
3 elements.
4 Write a program to add two 3×3 matrices
5 */
6 #include <stdio.h>
7 int main(){
8 int a[3][3], b[3][3], c[3][3], i,j;
9 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
10 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]);
11 for(i=0;i<3;i++){
12 for(j=0;j<3;j++){
13 c[i][j]=a[i][j]+b[i][j];
14 printf("%d ",c[i][j]);
15 }
localhost:53236/60823f66-851f-4e91-b5e7-e4a560f6f49a/ 2/5
25/09/2025, 21:26 Selected files

16 printf("\n");
17 }
18 }
19

q5.c

1 /*The product of two matrices A (m×n) and B (n×p) is another matrix C (m×p), where each
2 element is obtained by multiplying corresponding row and column elements.
3 Write a program to multiply two matrices (A: 3×2 and B: 2×3).*/
4 #include <stdio.h>
5 int main(){
6 int a[3][2], b[2][3], c[3][3], i,j,k;
7 for(i=0;i<3;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]);
8 for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]);
9 for(i=0;i<3;i++){
10 for(j=0;j<3;j++){
11 c[i][j]=0;
12 for(k=0;k<2;k++) c[i][j]+=a[i][k]*b[k][j];
13 printf("%d ",c[i][j]);
14 }
15 printf("\n");
16 }
17 }
18
19

q6.c

1 /*
2 An identity matrix is a square matrix in which all the diagonal elements are 1 and all other
3 elements are 0.
4 For example,
5 1 0 0
6 0 1 0
7 0 0 1
8 Write a program to check whether a given matrix is an identity matrix or not
9 */
10 #include <stdio.h>
11 int main(){
12 int a[3][3], i,j, flag=1;
13 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
14 for(i=0;i<3;i++)
15 for(j=0;j<3;j++)
16 if((i==j && a[i][j]!=1) || (i!=j && a[i][j]!=0)) flag=0;
17 if(flag) printf("identity");
18 else printf("not identity");
19 }
20

q7.c

localhost:53236/60823f66-851f-4e91-b5e7-e4a560f6f49a/ 3/5
25/09/2025, 21:26 Selected files

1 /*
2 In a square matrix, the elements above the main diagonal form the upper triangular part,
3 and the elements below the diagonal form the lower triangular part.
4 Write a program to display the upper triangular and lower triangular parts of a given 3×3
5 matrix separately
6 */
7 #include <stdio.h>
8 int main(){
9 int a[3][3], i,j;
10 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
11 printf("upper:\n");
12 for(i=0;i<3;i++){
13 for(j=0;j<3;j++) printf(i<=j? "%d ":"0 ",a[i][j]);
14 printf("\n");}
15 printf("lower:\n");
16 for(i=0;i<3;i++){
17 for(j=0;j<3;j++) printf(i>=j? "%d ":"0 ",a[i][j]);
18 printf("\n");
19 }
20 }
21

q8.c

1 /*
2 The secondary diagonal of a square matrix is formed by the elements where row index +
3 column index = n – 1.
4 For example, in a 3×3 matrix: elements (0,2), (1,1), and (2,0).
5 Write a program to find the sum of secondary diagonal elements of a given matrix
6 */
7 #include <stdio.h>
8 int main(){
9 int a[3][3], i,j,sum=0;
10 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
11 for(i=0;i<3;i++) sum+=a[i][2-i];
12 printf("Sum=%d",sum);
13 }
14

q9.c

1 /*
2 A saddle point is an element of the matrix which is the minimum in its row but maximum in
3 its column.
4 Write a program to check whether a given matrix contains a saddle point. If it exists, display
5 it; otherwise display “No Saddle Point”.
6 */
7 #include <stdio.h> // (took help in this question)
8 int main(){
9 int a[3][3], i,j,k, min, col, flag=0;
10 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
11 for(i=0;i<3;i++){
localhost:53236/60823f66-851f-4e91-b5e7-e4a560f6f49a/ 4/5
25/09/2025, 21:26 Selected files

12 min=a[i][0]; col=0;
13 for(j=1;j<3;j++) if(a[i][j]<min){ min=a[i][j]; col=j; }
14 int big=1;
15 for(k=0;k<3;k++) if(a[k][col]>min) big=0;
16 if(big){ printf("saddle=%d",min); flag=1; }
17 }
18 if(!flag) printf("no saddle");
19 }
20

q10.c

1 /*
2 A matrix can be printed in a spiral form by starting from the top-left corner, moving right,
3 then down, then left, and so on.
4 Write a program to print the elements of a given 3×3 matrix in spiral order
5 */
6 #include <stdio.h> //(took help in this question)
7 int main(){
8 int a[3][3], i,j, top=0,bot=2,l=0,r=2;
9 for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
10 while(top<=bot && l<=r){
11 for(i=l;i<=r;i++) printf("%d ",a[top][i]); top++;
12 for(i=top;i<=bot;i++) printf("%d ",a[i][r]); r--;
13 for(i=r;i>=l;i--) printf("%d ",a[bot][i]); bot--;
14 for(i=bot;i>=top;i--) printf("%d ",a[i][l]); l++;
15 }
16 }
17

localhost:53236/60823f66-851f-4e91-b5e7-e4a560f6f49a/ 5/5

You might also like