Thursday, December 1, 2016

C# program to prints an identity matrix using for loop

C# program which prints an identity matrix using for loop

Program statement:
Write a program which prints an identity matrix using for loop i.e. takes value n from user and show the identity table of size n * n.

Solution:
static void Main(string[] args)
{
int n,i,j;
Console.WriteLine("Enter value of n:");
n = Convert.ToInt32(Console.ReadLine());
int[,] arr=new int[n,n];

for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
arr[i, j] = 1;
else
arr[i, j] = 0;
Console.Write(arr[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}