23. sum of matrix
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace additionoftwomatrixexample
{
class Program
{
static void Main(string[] args)
{
int row, col;
int [,]a=new int[5,5];
int[,] b = new int[5, 5];
int[,] c = new int[5, 5];
Console.Write("Enter Number of Row:");
row = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter Number of Column:");
col = Convert.ToInt16(Console.ReadLine());
//Get Value For First Matrix i.e a[]
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write("Enter a[{0}][{1}]:", i, j);
a[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
// Print the First Matrix i.e. a[]
Console.WriteLine("First Matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write(a[i, j]+" ");
}
Console.WriteLine();
}
//Get Value For Second Matrix i.e b[]
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write("Enter b[{0}][{1}]:", i, j);
b[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
// Print the Second Matrix i.e. b[]
Console.WriteLine("Second Matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write(b[i, j]+" ");
}
Console.WriteLine();
}
// Perform Addition Logic Here
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
c[i, j] = a[i, j] + b[i, j];
}
}
Console.WriteLine();
//Print The Addition Matrix i.e c[]
Console.WriteLine("Addition Matrix");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write(c[i, j]+" ");
}
Console.WriteLine();
}
Console.Read();
}
}
}
OUTPUT:
Enter Number of Row:2
Enter Number of Column:2
Enter a[0][0]:1
Enter a[0][1]:2
Enter a[1][0]:3
Enter a[1][1]:4
First Matrix
1 2
3 4
Enter b[0][0]:1
Enter b[0][1]:2
Enter b[1][0]:3
Enter b[1][1]:4
Second Matrix
1 2
3 4
Addition Matrix
2 4
6 8
Comments
Post a Comment