C Program to Add Two Matrices using Array

Ex: Write a C program to add two matrices using array. How to write a C program to add two matrices using array. C program to add two matrices using array.

Input from user: 

Enter elements in 1st matrix of size 3x3: 

1 1 1
2 2 2
3 3 3

Enter elements in 2nd matrix of size 3x3:

4 4 4
3 3 3
2 2 2


Expected output:


Sum of matrices=

5 5 5
5 5 5
5 5 5



  • Quick links:
  1. What is array?
  2. For loop



  • Logic of matrix addition:

Matrix addition is done element wise. Means sum of matrices is defined by A+B= A↓ij + B↓ij.



C program to add two matrices using array,  Matrix addition using C programming language
C program to add two matrices using array




  Step by Step logic of the given program:


1. Accept two matrices from user declare varible say arr1 and arr2(use 2 dimensional array to store elements).


2. Store elements one by one in arr1 using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &arr1[row][col]);
        }
    }
Follow same these  steps for arr2.

3. add two matrices arr1+arr2 element wise using:

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
          arr3[row][col] = arr1[row][col] + arr2[row][col];
        }
    }

4. Last print sum of two matrices means resultant matrix arr3.






  C program to add two matrices using array:


#include<stdio.h>
#define size 3 // size of the matrix

int main()
{

    int arr1[size][size]; // 1st matrix
int arr2[size][size]; // 2nd Matrix
int arr3[size][size]; // Resultant matrix


    int row, col;

/* input elements in first matrix*/
printf("Enter elements in 1st matrix of size 3x3:\n");

    for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr1[row][col]);
}
}

/* input elements in second matrix */

    printf("\nEnter elements in 2nd matrix  of size 3x3: \n");

    for(row=0; row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr2[row][col]);
}
}


    /* Add two matrices arr1 and arr2 and store result in matrix arr3*/
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
    {
  arr3[row][col] = arr1[row][col] + arr2[row][col];
}
}


 /* Print the value of resultant matrix arr3 */ 
    printf("\nSum of matrices = \n");

    for(row=0; row<size;row++)
{
for(col=0; col<size;col++)
{
printf("%d ",arr3[row][col]);
}
printf("\n");
}

    return 0;

}


Above program shows the following output:


C program to add two matrices using array

Array's in C Exercises and Solutions

Array is collection of similar data types. You can store group of data of same data types using array. In array different data types elements are not allowed that simply means if we declare an integer array then we can store only integer type of elements in it.


There are two types of arrays in C:

1. One dimensional array.

2. Multi-dimensional array:

  • Two dimensional array.
  • Three dimensional array.
  • Four dimensional array etc...

C Program to Merge Two Arrays Sorted in Descending Order

Ex: Write a C program to merge two arrays and sort in descending order. How to write a C program to merge two arrays and sort in descending order. C program to merge two arrays and sort in descending order.

Input from user:

Enter the limit of first array: 3
Enter elements: 
1
2
3

Enter the limit of second array: 3
Enter elements:
1
2
3

C Program to Copy the Elements of one Array into Another Array

Ex: Write a C program to copy the elements of one array into another array. How to write a C program to copy the elements of one array into another array. C program to copy the elements of one array into another array.


Input from user:

Enter the limit: 5

Enter elements: 
1
3
5
7
9

Expected output:

Elements in 1st array are: 
arr[0]=1
arr[1]=3
arr[2]=5
arr[3]=7
arr[4]=9

C Program to Print Array Elements in Reverse Order

Ex: Write a C program to print given array elements in reverse order. How to write a C program to print array elements in reverse order. C program to print array elements in reverse order.

Input from user:

Enter the limit: 5

Enter the elements:
10
20
30
40
50

Expected output:

Given elements in reverse order:
50 40 30 20 10

What is Break Statement in C

Break statement is very useful to exit from any loop  such as for loop, while loop and do while loop. When the break statement is encountered in the loop then the loop will stop running the statements and immediately exit from the loop.



  Syntax of Break Statement in C :

Continue Statement in C

Continue statement skips some lines of code inside the loop and continues with the next iteration. 
When the continue statement is encounters in a loop, then program will skip the all the statements after the continue statement and the loop continues with the next iteration.
It is mainly used for a code which we want to skip.


  Syntax of Continue Statement in C :


C Program to Capitalize the Given String

Ex: Write a C program to capitalize a given string. How to write a C program to capitalize given string. C program to capitalize given string.


Input from user:

Enter the string: 
tHis IS cOdeForhuNger

Expected output:

String after capitalize: 
This Is Codeforhunger

C Program to Find Largest Element of Array

Ex: Write a C program to print largest element of array. How to write a C program to find largest element of array. C program to print largest element of array.


Input from user:


Enter the limit: 12


Enter elements: 

12 

34
-1
54
7
9
16
44
32
21
19

C Program to print Total number of Notes in given Amount

Ex: Write a C program to find total number of notes in given amount. How to write a C program to print total number of notes in given amount. C program to print total number of notes in given amount.


Input from user:


Enter the amount: 2528


Expected output:


500=5

100=0
50=0
20=1
10=0
5=1
2=1
1=1