Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Matrix
  • Practice Matrix
  • MCQs on Matrix
  • Tutorial on Matrix
  • Matrix Traversal
  • Sorting in Matrix
  • Matrix Rotation
  • Transpose of Matrix
  • Inverse of Matrix
  • Determinant of Matrix
  • Matrix Application
  • Adjoint & Inverse Matrix
  • Sparse Matrix
  • Matrix Exponentiation
Open In App
Next Article:
How to Reverse a String in C?
Next article icon

Add Matrix in C

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Matrices are the collection of numbers arranged in order of rows and columns. In this article, we will learn to write a C program for the addition of two matrices.

Addition of two matrices in c

The idea is to use two nested loops to iterate over each element of the matrices. The addition operation is performed by adding the corresponding elements of mat1[] and mat2[] and storing the result in the corresponding position of the resultant matrix.

C Program to Add Two Square Matrices

The below program adds two square matrices of size 4*4, we can change N for different dimensions.

C
// C program to implement // the above approach #include <stdio.h> #define N 4  // This function adds A[][] and B[][], // and stores the result in C[][] void add(int A[][N], int B[][N], int C[][N]) {     int i, j;     for (i = 0; i < N; i++)         for (j = 0; j < N; j++)             C[i][j] = A[i][j] + B[i][j]; }  // This function prints the matrix void printmatrix(int D[][N]) {     int i, j;     for (i = 0; i < N; i++) {         for (j = 0; j < N; j++)             printf("%d ", D[i][j]);         printf("\n");     } }  // Driver code int main() {     int A[N][N] = { { 1, 1, 1, 1 },                     { 2, 2, 2, 2 },                     { 3, 3, 3, 3 },                     { 4, 4, 4, 4 } };      int B[N][N] = { { 1, 1, 1, 1 },                     { 2, 2, 2, 2 },                     { 3, 3, 3, 3 },                     { 4, 4, 4, 4 } };      // To store result     int C[N][N];     int i, j;      printf("Matrix A is \n");     printmatrix(A);      printf("Matrix B is \n");     printmatrix(B);      add(A, B, C);      printf("Result matrix is \n");     printmatrix(C);      return 0; } 

Output
Matrix A is  1 1 1 1  2 2 2 2  3 3 3 3  4 4 4 4  Matrix B is  1 1 1 1  2 2 2 2  3 3 3 3  4 4 4 4  Result matrix is  2 2 2 2  4 4 4 4  6 6 6 6  8 8 8 8  

Complexity Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space: O(n2)

C Program to Add Two Rectangular Matrices

Below is the C program to add two rectangular matrices.

C
// C program to implement // the above approach #include <stdio.h> #define M 4 #define N 3  // This function adds A[][] and B[][], // and stores the result in C[][] void add(int A[M][N], int B[M][N], int C[M][N]) {     int i, j;     for (i = 0; i < M; i++)         for (j = 0; j < N; j++)             C[i][j] = A[i][j] + B[i][j]; }  // This function prints the matrix void printmatrix(int D[M][N]) {     int i, j;     for (i = 0; i < M; i++) {         for (j = 0; j < N; j++)             printf("%d ", D[i][j]);         printf("\n");     } }  // Driver code int main() {     int A[M][N] = {         { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 }     };      int B[M][N] = {         { 2, 1, 1 }, { 1, 2, 2 }, { 2, 3, 3 }, { 3, 4, 4 }     };      printf("Matrix A is \n");     printmatrix(A);      printf("Matrix B is \n");     printmatrix(B);      // To store result     int C[M][N];     int i, j;     add(A, B, C);      printf("Result matrix is \n");     printmatrix(C);      return 0; } 

Output
Matrix A is  1 1 1  2 2 2  3 3 3  4 4 4  Matrix B is  2 1 1  1 2 2  2 3 3  3 4 4  Result matrix is  3 2 2  3 4 4  5 6 6  7 8 8  

Complexity Analysis

  • Time Complexity: O(m*n)
  • Auxiliary Space: O(m*n)

Refer to the complete article Program for addition of two matrices for more details!

Related Articles

  • How to pass a 2D array as a parameter in C?


Next Article
How to Reverse a String in C?
author
kartik
Improve
Article Tags :
  • C Language
  • C Programs
  • DSA
  • Matrix
  • C Array Programs
Practice Tags :
  • Matrix

Similar Reads

  • Matrix Multiplication in C
    A matrix is a collection of numbers organized in rows and columns, represented by a two-dimensional array in C. Matrices can either be square or rectangular. In this article, we will learn the multiplication of two matrices in the C programming language. ExampleInput: mat1[][] = {{1, 2}, {3, 4}} mat
    3 min read
  • sin() in C
    The sin() function in C is a standard library function to find the sine value of the radian angle. It is used to evaluate the trigonometric sine function of an angle. It takes the radian angle as input and returns the sin of that angle. It is defined inside <math.h> header file. Syntax of sin(
    1 min read
  • Return an Array in C
    In C, arrays are linear data structures that allow users to store the same data in consecutive memory locations. Returning an array in C can be a little complex because unlike C++, C does not support directly returning arrays from functions. In this article, we will learn how to return an array in C
    6 min read
  • C Program to Add Two Integers
    Given two integers, the task is to add these integer numbers and return their sum. Examples Input: a = 5, b = 3Output: 8Explanation: The sum of 5 and 3 is 8. Input: a = -2, b = 7Output: 5Explanation: The sum of -2 and 7 is 5. Add Two Numbers in CIn C, we can add two numbers easily using addition ope
    4 min read
  • How to Reverse a String in C?
    In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu
    2 min read
  • Reverse String in C
    In C, reversing a string means rearranging the characters such that the last character becomes the first, the second-to-last character becomes the second, and so on. In this article, we will learn how to reverse string in C. The most straightforward method to reverse string is by using two pointers
    3 min read
  • C Program to Add 2 Binary Strings
    Given two Binary Strings, we have to return their sum in binary form. Approach: We will start from the last of both strings and add it according to binary addition, if we get any carry we will add it to the next digit. Input: 11 + 11Output: 110[GFGTABS] C // C Program to Add 2 Binary Strings // and
    8 min read
  • Convert String to int in C
    In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language. Example: Input: "1234"Output: 1234Explanation
    6 min read
  • tan() Function in C
    tan() function in C programming is provided by the math.h header file and it is used to calculate the tangent of an angle x where x is represented in radians. This function returns the tangent of a given angle value in radians and it only works for right-angled triangles. Syntax of tan() double tan(
    2 min read
  • Getting started with C
    C language is a popular programming language that was developed in 1970 by Dennis Ritchie at Bell Labs. The C programming language was developed primarily to build the UNIX operating system. It is widely used because it is simple, powerful, efficient, and portable. Features of C Programming Language
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences