Addition of two matrix program

Matrix addition in c

#include<stdio.h>
#include<conio.h>
void AdditionTwoMatrix();
void main()
{
	AdditionTwoMatrix();
	getch();
}
void AdditionTwoMatrix()
{
	int rows;
	printf("How many rows do you want to use the matrix \n");
	scanf("%d",&rows);
	int columns;
	printf("How many columns do you want to use the matrix \n");
	scanf("%d",&columns);
	int firstMatrix[10][10];
	printf("Enter your first matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			scanf("%d",&firstMatrix[i][j]);
		}
	}
	int secondMatrix[10][10];
	printf("Enter your second matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			scanf("%d",&secondMatrix[i][j]);
		}
	}
	int additionMatrix[10][10];
	printf("Here, Addition of two matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			additionMatrix[i][j]=firstMatrix[i][j]+secondMatrix[i][j];
			printf("%d\t",additionMatrix[i][j]);
		}
		printf("\n");
	}
}

Output

How many rows do you want to use the matrix
2
How many columns do you want to use the matrix
3
Enter your first matrix
1 2 3
4 5 6
Enter your second matrix
1 1 2
2 2 4
Here, Addition of two matrix
2       3       5
6       7       10

Matrix addition in c++

#include<iostream>
#include<conio.h>
using namespace std;
class MatrixAddition{
public:
void AdditionTwoMatrix()
{
	int rows;
	cout<<"How many rows do you want to use the matrix \n";
	cin>>rows;
	int columns;
	cout<<"How many columns do you want to use the matrix \n";
	cin>>columns;
	int firstMatrix[10][10];
	cout<<"Enter your first matrix \n";
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			cin>>firstMatrix[i][j];
		}
	}
	int secondMatrix[10][10];
	cout<<"Enter your second matrix \n";
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			cin>>secondMatrix[i][j];
		}
	}
	int additionMatrix[10][10];
	cout<<"Here, Addition of two matrix \n";
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			additionMatrix[i][j]=firstMatrix[i][j]+secondMatrix[i][j];
			cout<<additionMatrix[i][j]<<" ";
		}
		cout<<"\n";
	}
}
};
void main()
{
	MatrixAddition ma;
	ma.AdditionTwoMatrix();
	getch();
}

Output

How many rows do you want to use the matrix
2
How many columns do you want to use the matrix
2
Enter your first matrix
2 3
4 5
Enter your second matrix
6 7
8 9
Here, Addition of two matrix
8 10
12 14

Matrix addition in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixAddition
{
class AdditionOfTwoMatrix
{
public void AdditionTwoMatrix()
{
	int rows;
	Console.Write("How many rows do you want to use the matrix \n");
	rows=Convert.ToInt32(Console.ReadLine());
	int columns;
	Console.Write("How many columns do you want to use the matrix \n");
	columns=Convert.ToInt32(Console.ReadLine());
	int[,] firstMatrix=new int[10,10];
	Console.Write("Enter your first matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			firstMatrix[i,j]=Convert.ToInt32(Console.ReadLine());
		}
	}
	int[,] secondMatrix=new int[10,10];
	Console.Write("Enter your second matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			secondMatrix[i,j]=Convert.ToInt32(Console.ReadLine());
		}
	}
	int[,] additionMatrix=new int[10,10];
	Console.Write("Here, Addition of two matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			additionMatrix[i,j]=firstMatrix[i,j]+secondMatrix[i,j];
			Console.Write(additionMatrix[i,j]+" ");
		}
        Console.WriteLine();
	}
}
        static void Main(string[] args)
        {
            AdditionOfTwoMatrix atm = new AdditionOfTwoMatrix();
            atm.AdditionTwoMatrix();
            Console.ReadLine();
        }
    }
}

Output

How many rows do you want to use the matrix
2
How many columns do you want to use the matrix
2
Enter your first matrix
2 3
4 5
Enter your second matrix
6 7
8 9
Here, Addition of two matrix
8 10
12 14

Matrix addition in java

import java.util.*;
public class Main
{
public static void AdditionTwoMatrix()
{
    Scanner sc=new Scanner(System.in);
	int rows;
	System.out.print("How many rows do you want to use the matrix \n");
	rows=sc.nextInt();
	int columns;
	System.out.print("How many columns do you want to use the matrix \n");
	columns=sc.nextInt();
	int[][] firstMatrix=new int[10][10];
	System.out.print("Enter your first matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			firstMatrix[i][j]=sc.nextInt();
		}
	}
	int[][] secondMatrix=new int[10][10];
	System.out.print("Enter your second matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			secondMatrix[i][j]=sc.nextInt();
		}
	}
	int[][] additionMatrix=new int[10][10];
	System.out.print("Here, Addition of two matrix \n");
	for(int i=0;i<rows;i++)
	{
		for(int j=0;j<columns;j++)
		{
			additionMatrix[i][j]=firstMatrix[i][j]+secondMatrix[i][j];
			System.out.print(additionMatrix[i][j]+" ");
		}
        System.out.println();
	}
}
	public static void main(String[] args) {
		AdditionTwoMatrix();
	}
}

Output

How many rows do you want to use the matrix 
2
How many columns do you want to use the matrix 
2
Enter your first matrix 
1 2
2 3
Enter your second matrix 
3 4
5 6
Here, Addition of two matrix 
4 6 
7 9

Write a comment


Name Email Message