#include<stdio.h>
void main(){
int r,c,i,j,a[100][100],b[100][100],sum[100][100];
printf("Enter number of rows : ");
scanf("%d",&r);
printf("Enter number of columns : ");
scanf("%d",&c);
printf("##### First Matrix ######
");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("Enter element a %d%d : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("##### Second Matrix ######
");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("Enter element a %d%d : ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
//additon of two matrix
for(i=0;i<r;i++){
for(j=0;j<c;j++){
sum[i][j] = a[i][j] + b[i][j];
}
}
//to show the resultant matrix
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d ",sum[i][j]);
if(j == c-1){
printf("
");
}
}
}
}
#include<stdio.h>
int main()
{
int n,m,i,j,a[100][100],b[100][100];
printf("Enter row and col:");
scanf("%d%d",&n,&m);
printf("Matrix A :
");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Matrix B :
");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("b[%d][%d] : ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("Sum of matrix :
");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",(a[i][j]+b[i][j]));
}
printf("
");
}
}
import java.util.Scanner;
public class MatrixAddition {
private static Scanner scanner = new Scanner(System.in);
private double [][] arr;
private int columns ;
private int raws ;
private static int numberOfMat = 1;
public void setRawAndColumns(int raws, int columns){
System.out.println("(Enter number of raws.)
");
raws = scanner.nextInt();
System.out.println("(Enter number of columns).
");
columns = scanner.nextInt();
SetMatrix(raws,columns);
}
private void SetMatrix(int r , int c){
this.raws = r;
this.columns = c;
this.arr = new double[raws][columns];
enterValues(this.raws,this.columns,this.arr);
double [][] nextMat = new double[this.raws][this.columns];
enterValues(this.raws,this.columns, nextMat);
calculateMatrix(this.arr,nextMat);
}
private double[][] enterValues(int r , int c, double[][]arr){
System.out.println("Enter Values for matrix #"+(this.numberOfMat));
++this.numberOfMat;
for(int i = 0 ; i < arr.length ; ++i ){
for(int j = 0; j < arr[0].length ; ++j ){
System.out.println("raw #"+(i+1)+" Column #"+(j+1));
arr[i][j]= scanner.nextDouble();
}
}
return arr;
}
private double[][] calculateMatrix(double[][] mat,double nextMat[][]){
double [] [] sum = new double[this.raws][this.columns];
for(int i = 0 ; i < mat.length ; ++i){
for(int j = 0 ; j < mat[0].length ; ++j){
sum[i][j]+= (mat[i][j]+nextMat[i][j]);
}
}
printmatrix(sum);
return null;
}
private void printmatrix(double [][] sum){
for(int i = 0 ; i < sum.length; ++i){
System.out.print("[ ");
for (int j = 0 ; j < sum[0].length ; ++j){
System.out.print(sum[i][j]+" ");
}
System.out.println("]");
}
}
}