Home

Saturday, 16 July 2016

Find Inverse Of Matrix Using C#

Below Application find Inverse of Given Matrix .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a = new int[3, 3];
            int i, j;
            float determinant = 0;

            Console.WriteLine("Enter the 9 elements of matrix: ");
            for (i = 0; i < 3; i++)
                for (j = 0; j < 3; j++)
                    a[i, j] = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\nThe matrix is\n");
            for (i = 0; i < 3; i++)
            {
                Console.WriteLine("\n");
                for (j = 0; j < 3; j++)
                    Console.Write("{0}\t", a[i, j]);
            }

            for (i = 0; i < 3; i++)
                determinant = determinant + (a[0, i] * (a[1, (i + 1) % 3] * a[2, (i + 2) % 3] - a[1, (i + 2) % 3] * a[2, (i + 1) % 3]));

            Console.WriteLine("\nInverse of matrix is: \n\n");
            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                    Console.Write("{0}\t", ((a[(i + 1) % 3, (j + 1) % 3] * a[(i + 2) % 3, (j + 2) % 3]) - (a[(i + 1) % 3, (j + 2) % 3] * a[(i + 2) % 3, (j + 1) % 3])) / determinant);
                Console.WriteLine("\n");
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment