codescracker


c

C Program to Add Two Matrices



« Previous Program Next Program »


Add Two Matrices in C

To add two matrices in C programming, you have to ask to the user to enter the elements of both the matrix, now start adding the two matrix to form a new matrix. After adding two matrices display the third matrix which is the result of the addition of the two matrices

C Programming Code to Add Two Matrices

Following C program adds two 3*3 matrices to form the third matrix :

/* C Program - Add Two Matrices */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
	printf("Enter matrix 1 elements :");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			scanf("%d",&mat1[i][j]);
		}
	}
	printf("Enter matrix 2 elements :");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			scanf("%d",&mat2[i][j]);
		}
	}
	printf("Adding the two matrix to form the third matrix .....\n");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			mat3[i][j]=mat1[i][j]+mat2[i][j];
		}
	}
	printf("The two matrix added successfully...!!");
	printf("The new matrix will be :\n");
	for(i=0; i<3; i++)
	{
		for(j=0; j<3; j++)
		{
			printf("%d ",mat3[i][j]);
		}
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program to add two matrices

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test