C# - Identity Matrix with a for loop

Created by
Saturday, June 4, 2016

 

In this simple program we  print an identity matrix to the console using a for loop.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("www.dotnetdevelopernetwork.com");
 
        Console.WriteLine("Enter the Size of the Identity Matrix: ");
        int size;
        Int32.TryParse(Console.ReadLine(), out size);
 
        Console.WriteLine("Result: ");
 
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                if (i == j)
                {
                    Console.Write(1);
                }
                else
                {
                    Console.WriteLine(0);
                }
            }
 
            Console.WriteLine("\n");
        }
 
        Console.ReadKey();
    }
}
Tags:
c#
0
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments