0% found this document useful (0 votes)
43 views

Add and Multiply Operation Using C#

This document contains C# code to perform addition and multiplication of two matrices. The code first takes input for the number of rows and columns of the matrices from the user. It then takes input to populate the first and second matrices. For addition, it iterates through the matrices and adds the elements at the same index to calculate the sum matrix. For multiplication, it iterates through the rows of the first matrix and columns of the second to calculate the product of the elements and store in the sum matrix. Finally, it displays the result matrix.

Uploaded by

Sanaullah Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Add and Multiply Operation Using C#

This document contains C# code to perform addition and multiplication of two matrices. The code first takes input for the number of rows and columns of the matrices from the user. It then takes input to populate the first and second matrices. For addition, it iterates through the matrices and adds the elements at the same index to calculate the sum matrix. For multiplication, it iterates through the rows of the first matrix and columns of the second to calculate the product of the elements and store in the sum matrix. Finally, it displays the result matrix.

Uploaded by

Sanaullah Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment Topic: Addition and Multiplication of two Matrix using any

programming language.

Submitted By:

Atif Chaudhry

MSCS-1st Semester

Submitted To:

Prof. Dr. Kashif Hanif

GOVERNMENT COLLEGE UNIVERSITY FAISALABAD


Add Operation Code in C# :

public void Add_operations()


{
//Exception handling if getting error give message
try
{
int row, col, i, j;
int[,] mat = new int[100, 100];
int[,] mat2 = new int[100, 100];
int[,] sum = new int[100, 100];
Console.WriteLine("Enter Number of Rows for matrix ");
row = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Number of columns for matrix
");
col = Convert.ToInt32(Console.ReadLine());

//Getting Values in Matrix One


for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.WriteLine("Please Enter Value of
Matrix one at Index:" + i + 1 + " " + j + 1);
mat[i, j] =
Convert.ToInt32(Console.ReadLine());
}
}
//Getting Value in 2nd Matrix
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.WriteLine("Please Enter Value
Matrix two at Index:" + i + 1 + " " + j + 1);
mat2[i, j] =
Convert.ToInt32(Console.ReadLine());
}
}
//Adding 2 Matrix
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
sum[i, j] = mat[i, j] + mat2[i, j];
}
}
//Display marix
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.Write(sum[i, j]);
Console.WriteLine("");
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}

Multiply Two Matrix Code in C# :

public void Multiply_operation()


{
try
{

int row, col, i, j;


int[,] mat = new int[200, 200];
int[,] mat2 = new int[200, 200];
int[,] sum = new int[200, 200];

Console.WriteLine("Enter Number of Rows for


matrix :");
row = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Number of columns for matrix:
");
col = Convert.ToInt32(Console.ReadLine());
//Getting Values in Matrix One
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.WriteLine("Please Enter Value of
Matrix one at Index:" + i + 1 + " " + j + 1);
mat[i, j] =
Convert.ToInt32(Console.ReadLine());
}
}
//Getting Value in 2nd Matrix
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.WriteLine("Please Enter Value Matrix
two at Index:" + i + 1 + " " + j + 1);
mat2[i, j] =
Convert.ToInt32(Console.ReadLine());
}
}
//Multiply 2 Matrix
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
sum[i, j] += mat[i, j] * mat2[i, j];
}
}
//Display marix
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.Write(sum[i, j]);
Console.WriteLine("");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

You might also like