Home>
Blogs>

Addition of Two Matrix in C: A Step-By-Step Guide

Addition of Two Matrix in C: A Step-By-Step Guide

Tanya Verma
Tanya Verma
Published On - May 06, 2025 02:18PM IST

Are you finding it hard to understand the addition of two matrix in C? Don't worry! This simple guide will help you learn this basic concept. Matrix addition is something you need to know when working with arrays in C. In this guide, we will show you how to write a C program for matrix addition with clear steps. By the end, you will know how to add two matrices in your own C programs.

Table of Content

What Are Matrices?

Before we learn about addition of two matrix in C, let's understand what matrices are.

A matrix is just a group of numbers set up in rows and columns. In C, we use 2D arrays to make matrices. We can find any number in the matrix using two numbers - one for the row and one for the column.

For example, a 2×2 matrix looks like this:

[ a b ]

[ c d ]

Where a, b, c, and d are the numbers in the matrix.

C Program for Addition of Two Matrix

addition-of-two-matrix-in-c-addition-of-two-matrix-in-c

The addition of two matrix in C is very simple: we add the matching numbers from both matrices to make a new matrix of the same size.

For matrix addition to work, both matrices must be the same size (same number of rows and columns). If Matrix A is 3×3 and Matrix B is 3×3, we can add them. But if Matrix A is 3×3 and Matrix B is 2×2, we cannot add them.

Here's a simple example of the sum of two matrix in C:

Matrix A:      Matrix B:      Result (A + B):

[ 1  2 ]       [ 5  6 ]       [ 6  8 ]

[ 3  4 ]       [ 7  8 ]       [ 10 12 ]

C Program to add Two Matrices

Now, let's write a program for addition of two matrix. We'll break this down into easy steps:

Step 1: Set up the matrices

First, we need to make the matrices and a result matrix to store the sum.

#include <stdio.h>

int main() {

    int rows, cols;

   

    // Ask user for matrix size

    printf("Enter number of rows: ");

    scanf("%d", &rows);

    printf("Enter number of columns: ");

    scanf("%d", &cols);

    

    int matrixA[rows][cols], matrixB[rows][cols], sum[rows][cols];

    

    // Rest of code will go here

    

    return 0;

}

Step 2: Get numbers for both matrices

Next, we need to get the numbers for both matrices from the user.

// Get numbers for Matrix A

printf("\nEnter numbers for Matrix A:\n");

for(int i = 0; i < rows; i++) {

    for(int j = 0; j < cols; j++) {

        printf("Enter number A[%d][%d]: ", i, j);

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

    }

}

// Get numbers for Matrix B

printf("\nEnter numbers for Matrix B:\n");

for(int i = 0; i < rows; i++) {

    for(int j = 0; j < cols; j++) {

        printf("Enter number B[%d][%d]: ", i, j);

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

    }

}

Step 3: Add the matrices

Now for the main part - the addition of 2 matrix. We use loops to add matching numbers.

// Add matrices A and B

for(int i = 0; i < rows; i++) {

    for(int j = 0; j < cols; j++) {

        sum[i][j] = matrixA[i][j] + matrixB[i][j];

    }

}

Step 4: Show the result

Finally, we'll show the result matrix.

// Show the sum matrix

printf("\nSum of the two matrices:\n");

for(int i = 0; i < rows; i++) {

    for(int j = 0; j < cols; j++) {

        printf("%d\t", sum[i][j]);

    }

    printf("\n");

}

Full C Program for Matrix Addition

Here's the complete C program for addition of two matrix:

#include <stdio.h>

int main() {

    int rows, cols;

    

    // Ask user for matrix size

    printf("Enter number of rows: ");

    scanf("%d", &rows);

    printf("Enter number of columns: ");

    scanf("%d", &cols);

    

    int matrixA[rows][cols], matrixB[rows][cols], sum[rows][cols];

    

    // Get numbers for Matrix A

    printf("\nEnter numbers for Matrix A:\n");

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            printf("Enter number A[%d][%d]: ", i, j);

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

        }

    }

    

    // Get numbers for Matrix B

    printf("\nEnter numbers for Matrix B:\n");

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            printf("Enter number B[%d][%d]: ", i, j);

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

        }

    }

    

    // Add matrices A and B

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            sum[i][j] = matrixA[i][j] + matrixB[i][j];

        }

    }

    

    // Show the sum matrix

    printf("\nSum of the two matrices:\n");

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            printf("%d\t", sum[i][j]);

        }

        printf("\n");

    }

    

    return 0;

}

Adding Two 2×2 Matrices: An Example

Let's see how this addition of two matrix in C works with a simple example:

Input:

Enter number of rows: 2

Enter number of columns: 2

Enter numbers for Matrix A:

Enter number A[0][0]: 1

Enter number A[0][1]: 2

Enter number A[1][0]: 3

Enter number A[1][1]: 4

Enter numbers for Matrix B:

Enter number B[0][0]: 5

Enter number B[0][1]: 6

Enter number B[1][0]: 7

Enter number B[1][1]: 8

Output:

Sum of the two matrices:

6   8

10  12

How to Add Two 3×3 Matrices in C

The same program can be used to add two 3×3 matrices. Let's look at a simple example:

Input:

Enter number of rows: 3

Enter number of columns: 3

Enter numbers for Matrix A:

Enter number A[0][0]: 1

Enter number A[0][1]: 2

Enter number A[0][2]: 3

Enter number A[1][0]: 4

Enter number A[1][1]: 5

Enter number A[1][2]: 6

Enter number A[2][0]: 7

Enter number A[2][1]: 8

Enter number A[2][2]: 9

Enter numbers for Matrix B:

Enter number B[0][0]: 9

Enter number B[0][1]: 8

Enter number B[0][2]: 7

Enter number B[1][0]: 6

Enter number B[1][1]: 5

Enter number B[1][2]: 4

Enter number B[2][0]: 3

Enter number B[2][1]: 2

Enter number B[2][2]: 1

Output:

Sum of the two matrices:

10  10  10

10  10  10

10  10  10

A Better C Program for Matrix Addition

Here's a better C program for the addition of two matrix in C that checks for errors:

#include <stdio.h>

int main() {

    int r1, c1, r2, c2;

    

    // Get size of first matrix

    printf("Enter rows and columns for first matrix: ");

    scanf("%d %d", &r1, &c1);

    

    // Get size of second matrix

    printf("Enter rows and columns for second matrix: ");

    scanf("%d %d", &r2, &c2);

    

    // Check if we can add the matrices

    if(r1 != r2 || c1 != c2) {

        printf("Error! We cannot add these matrices. They must be the same size.\n");

        return 1;

    }

    

    int first[r1][c1], second[r2][c2], sum[r1][c1];

    

    // Get first matrix

    printf("\nEnter numbers for first matrix:\n");

    for(int i = 0; i < r1; i++) {

        for(int j = 0; j < c1; j++) {

            printf("Enter number [%d][%d]: ", i, j);

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

        }

    }

    

    // Get second matrix

    printf("\nEnter numbers for second matrix:\n");

    for(int i = 0; i < r2; i++) {

        for(int j = 0; j < c2; j++) {

            printf("Enter number [%d][%d]: ", i, j);

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

        }

    }

    

    // Add the matrices

    for(int i = 0; i < r1; i++) {

        for(int j = 0; j < c1; j++) {

            sum[i][j] = first[i][j] + second[i][j];

        }

    }

    

    // Show the result

    printf("\nSum of the matrices:\n");

    for(int i = 0; i < r1; i++) {

        for(int j = 0; j < c1; j++) {

            printf("%d\t", sum[i][j]);

        }

        printf("\n");

    }

    

    return 0;

}

This better version checks if the matrices can be added before trying to add them.

Using Functions for Matrix Addition

We can also write a program to add two matrices using functions:

#include <stdio.h>

// Function to get a matrix

void getMatrix(int matrix[][10], int rows, int cols) {

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            printf("Enter number [%d][%d]: ", i, j);

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

        }

    }

}

// Function to add two matrices

void addMatrices(int first[][10], int second[][10], int result[][10], int rows, int cols) {

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            result[i][j] = first[i][j] + second[i][j];

        }

    }

}

// Function to show a matrix

void showMatrix(int matrix[][10], int rows, int cols) {

    for(int i = 0; i < rows; i++) {

        for(int j = 0; j < cols; j++) {

            printf("%d\t", matrix[i][j]);

        }

        printf("\n");

    }

}

int main() {

    int first[10][10], second[10][10], result[10][10];

    int rows, cols;

    

    printf("Enter number of rows: ");

    scanf("%d", &rows);

    printf("Enter number of columns: ");

    scanf("%d", &cols);

    

    printf("\nEnter numbers for first matrix:\n");

    getMatrix(first, rows, cols);   

    printf("\nEnter numbers for second matrix:\n");

    getMatrix(second, rows, cols);

    

    // Add matrices

    addMatrices(first, second, result, rows, cols);

    

    // Show result

    printf("\nSum of the matrices:\n");

    showMatrix(result, rows, cols);

    

    return 0;

}

Common Problems in Matrix Addition

When writing a C program for matrix addition, you might face these common problems:

  1. Wrong size: Trying to add matrices of different sizes.

  2. Array problems: Trying to use numbers outside the array size.

  3. Memory problems: Not having enough memory for big matrices.

  4. Input problems: Not checking if the input is correct.

How Matrix Addition Is Used

The addition of two matrix in C has many uses:

  • Picture work: Pictures can be shown as matrices, and many picture changes use matrix addition.

  • Computer drawing: Many drawing changes use matrix math.

  • Science work: Physics, building, and many sciences use matrices for math.

  • Machine learning: Computer learning uses a lot of matrix math.

Conclusion

Addition of two matrix in C or three matrices is a basic operation that's easy to do. Here's a quick review:

  1. Matrix addition needs both matrices to be the same size.

  2. Each number in the result matrix is the sum of matching numbers from the input matrices.

  3. We use loops to go through all the matrix numbers.

  4. Using functions makes the code easier to understand.

Matrix work is a big part of many programs, so knowing how to do it will make you a better coder.

Frequently Asked Questions

1. What is matrix addition in C?

Matrix addition in C is when you add two matrices by adding each matching number to make a new matrix of the same size.

2. How to add matrices 2x2 and 2x2?

For addition of two matrix in c, add the matching numbers. If A = [[1,2],[3,4]] and B = [[5,6],[7,8]], then A+B = [[6,8],[10,12]].

3. How to add two 3x3 matrices in C?

Adding two 3x3 matrices works just like adding any matrices: add the matching numbers from both to make a new 3x3 matrix.

4. What is matrix 4 called?

A 4×4 matrix is often called a "4 by 4 matrix." These are used a lot in 3D computer drawing.

5. What is matrix coding?

Matrix coding means writing code to work with matrices in languages like C. It means showing matrices as 2D arrays and doing math with them.

6. How to find the inverse of a matrix?

Finding the inverse of a matrix needs several steps and is harder than adding. It needs a different set of steps.

7. What are singular matrices?

Singular matrices are square matrices with a special number (called determinant) that equals zero. These matrices don't have an inverse.

Latest Blogs
srm-university-btech-fees-structure-2026.webp

SRM University BTech Fees Structure 2026

SRM University BTech Fees Structure 2026: Course-wise FeesPublished On - Apr 23, 2026
iit-madras-mtech-admission-eligibility-and-process.webp

IIT Madras MTech Admission: Eligibility and Process

IIT Madras MTech Admission: Eligibility and ProcessPublished On - Apr 23, 2026
life-inside-iits-in-india-academics-culture-and-opportunities.webp

Life Inside IITs in India: Academics, Culture, and Opportunities

Life Inside IITs in India: Academics & Campus LifePublished On - Apr 23, 2026
admission-process-at-amity-university-noida.webp

What is the Admission Process at Amity University Noida

What is the Admission Process at Amity University NoidaPublished On - Apr 23, 2026
Get Latest Updates

Like what you see? Share this news on :

Share on FacebookShare on TwitterShare on LinkedInShare on WhatsAppVisit us on Instagram
Top IIMs
IIM AhmedabadIIM BangaloreIIM IndoreIIM CalcuttaIIM KozhikodeIIM MumbaiIIM RohtakIIM JammuIIM KashipurIIM LucknowIIM VisakhapatnamIIM RaipurIIM RanchiIIM SambalpurIIM ShillongIIM SirmaurIIM TiruchirappalliIIM NagpurIIM AmritsarIIM UdaipurIIM BodhGaya
Top IITs
IIT MadrasIIT DelhiIIT BombayIIT KharagpurIIT KanpurIIT RoorkeeIIT GuwahatiIIT HyderabadIIT BHUIIT IndoreIIT Tirupati
Exams
CATXATMATCMATGMAT
MBA Colleges (Cities)
MBA Colleges in BangaloreMBA Colleges in HyderabadMBA Colleges in MumbaiMBA Colleges in PuneMBA Colleges in AhmedabadMBA Colleges in ChennaiMBA Colleges in IndoreMBA Colleges in CoimbatoreMBA Colleges in NoidaMBA Colleges in GhaziabadMBA Colleges in Chandigarh
MBA Colleges (State)
MBA Colleges in DelhiMBA Colleges in KeralaMBA Colleges in GujaratMBA Colleges in MaharashtraMBA Colleges in OdishaMBA Colleges in Uttar PradeshMBA Colleges in BiharMBA Colleges in KarnatakaMBA Colleges in Madhya PradeshMBA Colleges in UttarakhandMBA Colleges in Haryana
PGDM Colleges (Cities)
PGDM Colleges in PunePGDM Colleges in MumbaiPGDM Colleges in BangalorePGDM Colleges in HyderabadPGDM Colleges in AhmedabadPGDM Colleges in IndorePGDM Colleges in KolkataPGDM Colleges in NoidaPGDM Colleges in JaipurPGDM Colleges in VisakhapatnamPGDM Colleges in Chennai
PGDM Colleges (States)
PGDM Colleges in DelhiPGDM Colleges in KeralaPGDM Colleges in MaharashtraPGDM Colleges in HaryanaPGDM Colleges in UttarakhandPGDM Colleges in GujaratPGDM Colleges in KarnatakaPGDM Colleges in PunjabPGDM Colleges in RajasthanPGDM Colleges in Uttar PradeshPGDM Colleges in West Bengal
Top Management Colleges
Institute of Management Technology, GhaziabadFaculty of Management StudiesManagement Development Institute, GurgaonSP Jain Institute of Management & ResearchIndian School of Business, HyderabadChrist University, BangaloreICFAI Business School, HyderabadSymbiosis Institute of International BusinessInstitute of Management Studies, GhaziabadInstitute of Rural Management AnandBirla Institute of Management TechnologyUniversity of LucknowAlliance School of businessXavier Business SchoolPSG College of TechnologyXLRI JamshedpurVIT University, VelloreXIME, KochiShiv Nadar UniversitySIES College of Management StudiesInstitute of Management Technology, NagpurXavier Institute of Social Service
College Hai Logo
Visit CollegeHai on FacebookVisit CollegeHai on InstagramVisit CollegeHai on TwitterVisit CollegeHai on YouTubeVisit CollegeHai on LinkedIn

© 2026 CollegeHai | Unifyra Internet Pvt. Ltd. All rights reserved.

info@collegehai.com
+91-7838036126
About UsAbout CollegehaiContact UsTerms & ConditionsPrivacy PolicyLegal Terms
ResourcesBlogsNews
DiscoverStudy AbroadCollegesExamsCoursesCollege Finder
Follow us on :
Visit CollegeHai on FacebookVisit CollegeHai on InstagramVisit CollegeHai on TwitterVisit CollegeHai on YouTubeVisit CollegeHai on LinkedIn
Collegehai logo
Collegehai logo

Top Colleges in Engineering

  • Indian Institute of Technology Mumbai
  • Indian Institute of Technology Varanasi
  • Indian Institute of Technology Indore
  • Indian Institute of Technology Kanpur
  • Indian Institute of Technology Roorkee
  • Indian Institute of Technology Guwahati
  • Indian Institute of Technology Kharagpur
  • Indian Institute of Technology Jodhpur
  • Indian Institute of Technology Bhilai
  • Indian Institute of Technology Bhubaneswar
  • Indian Institute of Technology Hyderabad
  • Indian Institute of Technology Patna
  • Indian Institute of Technology Ropar
  • Indian Institute of Technology Palakkad
  • Indian Institute of Technology Dharwad
View All

Cities

  • Bangalore
  • New Delhi
  • Gurgaon
  • Indore
  • Mumbai
  • Nagpur

Study Abroad

  • Germany
  • Australia
  • United States
  • Singapore
  • United Kingdom

Courses

  • B.Tech
  • M.Tech
  • Ph.D

States

  • Tamil Nadu
  • Uttar Pradesh
  • Delhi
  • Karnataka
  • Punjab
  • Maharashtra

Exams

  • JEE Main 2026
  • JEE Advanced 2026
  • BITSAT 2026
  • VITEEE 2026
  • SRMJEEE 2026
  • CUET PG 2026

Discover

  • Explore Study AbroadStudy Abroad
  • Explore CollegesColleges
  • Explore ExamsExams
  • Explore CoursesCourses
  • Explore Online CollegesOnline Colleges
  • Explore Institutes Institutes
  • Explore NewsNews
  • Explore BlogsBlogs
  • Explore CompareCompareNew
  • Explore FinderFinderNew
QR Code
Collegehai logo
  • Explore Study AbroadStudy Abroad
  • Explore CollegesColleges
  • Explore ExamsExams
  • Explore CoursesCourses
  • Explore Online CollegesOnline Colleges
  • Explore Institutes Institutes
  • Explore NewsNews
  • Explore BlogsBlogs
  • Explore CompareCompareNew
  • Explore FinderFinderNew
QR Code