Vous êtes sur la page 1sur 33

/*

* C# Program to Perform Matrix Addition

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication8

class Program

public static void Main(string[] args)

int m, n,i,j;

Console.Write("Enter Number Of Rows And Columns Of Matrices A and B


: ");

m = Convert.ToInt16(Console.ReadLine());

n = Convert.ToInt16(Console.ReadLine());

int[,] A = new int[10, 10];

Console.Write("\nEnter The First Matrix : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

A[i, j] = Convert.ToInt16(Console.ReadLine());

int[,] B = new int[10, 10];


Console.Write("\nEnter The Second Matrix:");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

B[i, j] = Convert.ToInt16(Console.ReadLine());

Console.Clear();

Console.WriteLine("\nMatrix A : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(A[i, j] + "\t");

Console.WriteLine();

Console.WriteLine("\nMatrix B: ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(B[i, j] + "\t");

Console.WriteLine();

int[,] C = new int[10, 10];


for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

C[i, j] = A[i, j] + B[i, j];

Console.Write("\nSum Matrix :");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(C[i, j] + "\t");

Console.WriteLine();

Console.Read();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Perform Matrix Subtraction

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;
namespace Program

class Program

public static void Main(string[] args)

int m, n, i, j;

Console.Write("Enter Number Of Rows And Columns Of Matrices A and B


: ");

m = Convert.ToInt16(Console.ReadLine());

n = Convert.ToInt16(Console.ReadLine());

int[,] A = new int[10, 10];

Console.Write("\nEnter The First Matrix : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

A[i, j] = Convert.ToInt16(Console.ReadLine());

int[,] B = new int[10, 10];

Console.Write("\nEnter The Second Matrix:");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

B[i, j] = Convert.ToInt16(Console.ReadLine());

}
Console.Clear();

Console.WriteLine("\nMatrix A : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(A[i, j] + "\t");

Console.WriteLine();

Console.WriteLine("\nMatrix B: ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(B[i, j] + "\t");

Console.WriteLine();

int[,] C = new int[10, 10];

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

C[i, j] = A[i, j] - B[i, j];

}
Console.Write("\nDifference Matrix :");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(C[i, j] + "\t");

Console.WriteLine();

Console.Read();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Generate the Transpose of a Given Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Program

class Program

public static void Main(string[] args)

int m, n, i, j;
Console.Write("Enter the Order of the Matrix : ");

m = Convert.ToInt16(Console.ReadLine());

n = Convert.ToInt16(Console.ReadLine());

int[,] A = new int[10, 10];

Console.Write("\nEnter The Matrix Elements : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

A[i, j] = Convert.ToInt16(Console.ReadLine());

Console.Clear();

Console.WriteLine("\nMatrix A : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(A[i, j] + "\t");

Console.WriteLine();

Console.WriteLine("Transpose Matrix : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(A[j, i] + "\t");
}

Console.WriteLine();

Console.Read();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Interchange any 2 Rows of a Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class interchangerow

int m, n;

int[,] a;

public interchangerow(int x, int y)

m = x;

n = y;

a = new int[m, n];

public void readmatrix()

Console.WriteLine("Enter the Elements : ");

for (int i = 0; i < m; i++)


{

for (int j = 0; j < n; j++)

Console.WriteLine("a[{0},{1}]=", i, j);

a[i, j] = Convert.ToInt32(Console.ReadLine());

public void printmax()

Console.WriteLine("Given Matrix : ");

for (int i = 0; i < m; i++)

for (int j = 0; j < n; j++)

Console.Write("{0}\t", a[i, j]);

Console.WriteLine();

public void interchange()

Console.WriteLine("Enter the Row Number to Interchange : ");

int i = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the Row Number with which Interchange is


to be Accomplished :");

int j = Convert.ToInt32(Console.ReadLine());

for (int k = 0; k < n; k++)

{
int temp = a[i - 1, k];

a[i - 1, k] = a[j - 1, k];

a[j - 1, k] = temp;

public static void Main()

int x, y;

interchangerow obj;

Console.Write("Enter the Number of Rows");

x = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the Number of Columns");

y = Convert.ToInt32(Console.ReadLine());

obj = new interchangerow(x, y);

obj.readmatrix();

obj.printmax();

obj.interchange();

obj.printmax();

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Display Upper Triangular Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication8
{

class Program

int x;

public static void Main(string[] args)

int m, n, i, j;

Console.Write("Enter Number Of Rows And Columns Of Matrices A and B


: ");

m = Convert.ToInt16(Console.ReadLine());

n = Convert.ToInt16(Console.ReadLine());

int[,] A = new int[10, 10];

Console.Write("\nEnter The First Matrix : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

A[i, j] = Convert.ToInt16(Console.ReadLine());

Console.Clear();

Console.WriteLine("\nMatrix A : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(A[i, j] + "\t");

}
Console.WriteLine();

Console.WriteLine("\n Setting Zero to illustrate Upper Triangular


Matrix\n");

for (i = 0; i < m; i++)

Console.Write("\n");

for (j = 0; j < 3; j++)

if (i <= j)

Console.Write(A[i, j] + "\t");

else

Console.Write("0\t");

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Check If a Given Matrix is an Identity Matrix

*/

using System;

class pro

public static void Main()

Console.WriteLine("Enter the order: ");


int n = int.Parse(Console.ReadLine());

int[,] a = new int[3, 3];

int i, j;

Console.WriteLine("\n Enter the matrix\n");

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

a[i, j] = Convert.ToInt16(Console.ReadLine());

for (i = 0; i < n; i++)

for (j = 0; j < n; j++)

if ((i == j && a[i, j] != 1) || (i != j && a[i, j] != 0))

goto label;

Console.WriteLine("Identity Matrix");

return;

label:

Console.WriteLine("\n Not an Identity Matrix");

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*
* C# Program to Find Largest Element in a Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class arrsampl

int[,]x;

arrsampl()

x = new int[,] { { 12, 21, 63 }, { 40, 15, 6 } };

void printarray()

Console.WriteLine("Elements in the Given Matrix : ");

for (int i = 0; i < 2; i++)

for (int j = 0; j < 3; j++)

Console.Write(x[i, j] + "\t");

Console.WriteLine("\n");

int max()

int big = x[0, 0];

for (int i = 0; i < 2; i++)

{
for (int j = 0; j < 3; j++)

if (big < x[i, j])

big = x[i, j];

return big;

public static void Main()

arrsampl obj = new arrsampl();

obj.printarray();

Console.WriteLine("Largest Element : {0}", obj.max());

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Perform Matrix Multiplication

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace matrix_multiplication

class Program
{

static void Main(string[] args)

int i, j,m,n;

Console.WriteLine("Enter the Number of Rows and Columns : ");

m = Convert.ToInt32(Console.ReadLine());

n = Convert.ToInt32(Console.ReadLine());

int[,] a = new int[m, n];

Console.WriteLine("Enter the First Matrix");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

a[i, j] = int.Parse(Console.ReadLine());

Console.WriteLine("First matrix is:");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(a[i, j] + "\t");

Console.WriteLine();

int[,] b = new int[m, n];

Console.WriteLine("Enter the Second Matrix");

for (i = 0; i < m; i++)

{
for (j = 0; j < n; j++)

b[i, j] = int.Parse(Console.ReadLine());

Console.WriteLine("Second Matrix is :");

for (i = 0; i < 2; i++)

for (j = 0; j < 2; j++)

Console.Write(b[i, j] + "\t");

Console.WriteLine();

Console.WriteLine("Matrix Multiplication is :");

int[,] c = new int[m, n];

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

c[i, j] = 0;

for (int k = 0; k < 2; k++)

c[i, j] += a[i, k] * b[k, j];

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)


{

Console.Write(c[i, j] + "\t");

Console.WriteLine();

Console.ReadKey();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Find Sum of the Elements of each Row

* of the Given Matrix

*/

using System;

using System.Collections.Generic;

using System.Text;

namespace matrix_row_sum

class mat

int i, j, m, n;

int[,] a = new int[20, 20];

public void getmatrix()

Console.WriteLine("Enter The Number of Rows : ");

m = int.Parse(Console.ReadLine());

Console.WriteLine("Enter The Number of Columns : ");

n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Elements");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

a[i, j] = int.Parse(Console.ReadLine());

Console.WriteLine("Given Matrix");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

Console.Write("\t{0}", a[i, j]);

Console.WriteLine();

public void row()

int r;

for (i = 1; i <= m; i++)

r = 0;

for (j = 1; j <= n; j++)

r = r + a[i, j];

Console.WriteLine("{0} Row Sum : {1}", i, r);


}

class matrowsum

static void Main(string[] args)

mat ma = new mat();

ma.getmatrix();

ma.row();

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Find Sum of the Elements of each Column

* of the Given Matrix

*/

using System;

using System.Collections.Generic;

using System.Text;

namespace matrix_col

class mat

int i, j, m, n;

int[,] a = new int[20, 20];

int[,] c = new int[20, 20];

public void getmatrix()


{

Console.WriteLine("Enter the Number of Rows : ");

m = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the Number of Columns : ");

n = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the Elements");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

a[i, j] = int.Parse(Console.ReadLine());

Console.WriteLine("Given Matrix");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

Console.Write("\t{0}", a[i, j]);

Console.WriteLine();

public void col()

int c;

for (i = 1; i <= n; i++)

c = 0;
for (j = 1; j <= m; j++)

c = c + a[j, i];

Console.WriteLine("{0} Column Sum : {1}", i, c);

class matsum

static void Main(string[] args)

mat ma = new mat();

ma.getmatrix();

ma.col();

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Find Smallest Element in a Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class arrsampl

int[,]x;
arrsampl()

x = new int[,] { { 11, 2, 61 }, { 42, 50, 3 } };

void printarray()

Console.WriteLine("Elements in the Given Matrix : ");

for (int i = 0; i < 2; i++)

for (int j = 0; j < 3; j++)

Console.Write(x[i, j] + "\t");

Console.WriteLine("\n");

int max()

int small = x[0, 0];

for (int i = 0; i < 2; i++)

for (int j = 0; j < 3; j++)

if (small > x[i, j])

small = x[i, j];

}
return small;

public static void Main()

arrsampl obj = new arrsampl();

obj.printarray();

Console.WriteLine("Smallest Element : {0}", obj.max());

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Find the Sum of the Values on

* Diagonal of the Matrix

*/

using System;

using System.Collections.Generic;

using System.Text;

class mat

int i, j, m, n;

int[,] a = new int[20, 20];

public void get()

Console.WriteLine("Enter Row Value");

m = int.Parse(Console.ReadLine());

Console.WriteLine("Enter Column Value");

n = int.Parse(Console.ReadLine());

Console.WriteLine("Enter Elements one by one");

for (i = 1; i <= m; i++)


{

for (j = 1; j <= n; j++)

a[i, j] = int.Parse(Console.ReadLine());

Console.WriteLine("Given Matrix");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

Console.Write("\t{0}", a[i, j]);

Console.WriteLine();

public void diag()

int d;

d = 0;

if (m == n)

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

if (i == j)

d = d + a[i, j];
}

Console.WriteLine("Diagonal Sum= {0}", d);

else

Console.WriteLine("Can't Perform Diagonal Sum");

class matsum

static void Main(string[] args)

mat ma = new mat();

ma.get();

ma.diag();

Console.Read();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Interchange any 2 Columns of a Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;
class interchangecol

int m, n;

int[,] a;

public interchangecol(int x, int y)

m = x;

n = y;

a = new int[m, n];

public void readmatrix()

Console.WriteLine("Enter the Elements : ");

for (int i = 0; i < m; i++)

for (int j = 0; j < n; j++)

Console.WriteLine("a[{0},{1}] =", i, j);

a[i, j] = Convert.ToInt32(Console.ReadLine());

public void printmax()

Console.WriteLine("Given Matrix : ");

for (int i = 0; i < m; i++)

for (int j = 0; j < n; j++)

{
Console.Write("{0}\t", a[i, j]);

Console.WriteLine();

public void interchange()

Console.WriteLine("Enter the Column Number to Interchange : ");

int i = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the Column Number with which Interchange is to


be Accomplished :");

int j = Convert.ToInt32(Console.ReadLine());

for (int k = 0; k < m; k++)

int temp = a[k, i-1];

a[k, i-1] = a[k, j-1];

a[k, j-1] = temp;

public static void Main()

int x, y;

interchangecol obj;

Console.Write("Enter the Number of Rows");

x = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the Number of Columns");

y = Convert.ToInt32(Console.ReadLine());

obj = new interchangecol(x, y);

obj.readmatrix();

obj.printmax();
obj.interchange();

obj.printmax();

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Find the Sum of the Values on

* Diagonal of the Matrix

*/

using System;

using System.Collections.Generic;

using System.Text;

class mat

int i, j, m, n;

int[,] a = new int[20, 20];

public void get()

Console.WriteLine("Enter Row Value");

m = int.Parse(Console.ReadLine());

Console.WriteLine("Enter Column Value");

n = int.Parse(Console.ReadLine());

Console.WriteLine("Enter Elements one by one");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

a[i, j] = int.Parse(Console.ReadLine());

}
}

Console.WriteLine("Given Matrix");

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

Console.Write("\t{0}", a[i, j]);

Console.WriteLine();

public void diag()

int d;

d = 0;

if (m == n)

for (i = 1; i <= m; i++)

for (j = 1; j <= n; j++)

if (i == j)

d = d + a[i, j];

Console.WriteLine("Diagonal Sum= {0}", d);


}

else

Console.WriteLine("Can't Perform Diagonal Sum");

class matsum

static void Main(string[] args)

mat ma = new mat();

ma.get();

ma.diag();

Console.Read();

}
-----------------------------------------------------------------------------
----------------------------------------------------
/*

* C# Program to Display Lower Triangular Matrix

*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication8

class Program

int x;
public static void Main(string[] args)

int m, n, i, j;

Console.Write("Enter Number Of Rows And Columns Of Matrices A and B


: ");

m = Convert.ToInt16(Console.ReadLine());

n = Convert.ToInt16(Console.ReadLine());

int[,] A = new int[10, 10];

Console.Write("\nEnter The First Matrix : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

A[i, j] = Convert.ToInt16(Console.ReadLine());

Console.Clear();

Console.WriteLine("\nMatrix A : ");

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

Console.Write(A[i, j] + "\t");

Console.WriteLine();

Console.WriteLine("\n Setting Zero to illustrate Lower Triangular


Matrix\n");

for (i = 0; i < m; i++)

Console.Write("\n");

for (j = 0; j < 3; j++)

if (i >= j)

Console.Write(A[i, j] + "\t");

else

Console.Write("0\t");

Console.ReadLine();

}
-----------------------------------------------------------------------------
----------------------------------------------------

Vous aimerez peut-être aussi