# Matrix program in c using array

## Matrix program in c using array

Here all the Matrix programs in c using array are available. In this program, we will add two matrices, subtract two matrices and multiply two matrices in c using an array.

## Addition of matrices in c

In this Matrix program in c using array, we will add two matrices. First, we insert numbers in the matrix entered by the user in Matrix A and Matrix B using for loop. Now we take another matrix that contains the addition of corresponding elements of matrices using for loop.

``` #include<stdio.h>
#include<conio.h>
int main()
{
int C,A,B,i,j,ch;
clrscr();
printf(“\n Matrix A\n”);
printf(“\n Enter the numbers you want to insert in the matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
A[i][j]=ch;
}
}
printf(“\n Matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,A[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n\n Matrix B”);
printf(“\n Enter the numbers you want to insert in the matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
B[i][j]=ch;
}
}
printf(“\n Matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,B[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n A+B=\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
C[i][j]= A[i][j]+B[i][j];
printf(“%d”,C[i][j]);
printf(“\t”);
}
printf(“\n”);
}
return 0;}
```

Approach to make C program for addition of matrices:
1. Using Nested for loop, we initialize array elements in two matrices A & B.
2. Using Nested for loop, we print all the numbers entered by the user on the screen.
3. Using Nested for loop, we add the corresponding elements of matrices. For this we use the following statement:
C[i][j]= A[i][j]+B[i][j];

## Subtraction of two matrices in c

In this matrix program in c using array, we will subtract two matrices. First, we insert numbers in the matrix entered by the user in Matrix A and Matrix B using for loop. Now we take another matrix that contains subtraction of corresponding elements of matrices using for loop.

``` #include<stdio.h>
#include<conio.h>
int main()
{
int C,A,B,i,j,ch;
clrscr();
printf(“\n Matrix A\n”);
printf(“\n Enter the numbers”);
printf(“\n you want to insert”);
printf(“ in the matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
A[i][j]=ch;
}
}
printf(“\n Matrix A:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,A[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n\n Matrix B”);
printf(“\n Enter the numbers”);
printf(“\n you want to insert”);
printf(“ in the matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“\t”);
scanf(“%d”,&ch);
B[i][j]=ch;
}
}
printf(“\n Matrix B:\n”);
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf(“%d”,B[i][j]);
printf(“\t”);
}
printf(“\n”);
}
printf(“\n A-B=\n”);

for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
C[i][j]= A[i][j]-B[i][j];
printf(“%d”,C[i][j]);
printf(“\t”);
}
printf(“\n”);
}
return 0;
}
```
Approach to make C program for subtraction of matrices:
1. Using Nested for loop, we insert the matrices element.
2. Using Nested for loop, we print all the numbers entered by the user on the screen.
3. Using Nested for loop, we subtract the corresponding elements of matrices. For this we use the following statement:
C[i][j]= A[i][j]-B[i][j];

## Matrix program in c using array

Matrix- A matrix is an ordered array of numbers. We can perform arithmetic operations like addition, subtraction, and multiplication of matrices only in a square matrix. Square matrices are those which have the same number of rows and columns.
Following are the examples of the square matrix: -

Matrix A
A A A
A A A
A A A

Matrix B
B B B
B B B
B B B

Matrix C
C C C
C C C
C C C

To make a Matrix program in c using an array, we use nested for loop.
Questions and Answers

How do you declare a 2d array?

int A[r][c];
where r is the number of rows
and c is the number of columns

What is a two-dimensional array?

A two-dimensional array is a matrix with several rows and columns.

for example:
A A A
A A A
A A A

What is a square matrix?

Square matrices are those which have the same number of rows and columns. Following are the examples of the square matrix:-

Matrix A
A A A
A A A
A A A

These are all the Matrix program in c using an array. Thanks for reading this program if you have any query you can comment below and also you can contact us.

Programs you must like:

1. Menu driven program in c • Software Developer • Website Developer • Masters in SEO • Social Media Manager

### 2 comments

1. 2. Please provide the source code of array operations like addition of elements in an array, deletion and traversal.