Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.*;
  2.  
  3.  
  4.  
  5. public class Invertible_Matrix
  6.  
  7. {
  8.  
  9.     public double determinant(double A[][],int N)
  10.  
  11.     {
  12.  
  13.         double det=0;
  14.  
  15.         if(N == 1)
  16.  
  17.         {
  18.  
  19.             det = A[0][0];
  20.  
  21.         }
  22.  
  23.         else if (N == 2)
  24.  
  25.         {
  26.  
  27.             det = A[0][0]*A[1][1] - A[1][0]*A[0][1];
  28.  
  29.         }
  30.  
  31.         else
  32.  
  33.         {
  34.  
  35.             det=0;
  36.  
  37.             for(int j1=0;j1<N;j1++)
  38.  
  39.             {
  40.  
  41.                 double[][] m = new double[N-1][];
  42.  
  43.                 for(int k=0;k<(N-1);k++)
  44.  
  45.                 {
  46.  
  47.                     m[k] = new double[N-1];
  48.  
  49.                 }
  50.  
  51.                 for(int i=1;i<N;i++)
  52.  
  53.                 {
  54.  
  55.                     int j2=0;
  56.  
  57.                     for(int j=0;j<N;j++)
  58.  
  59.                     {
  60.  
  61.                         if(j == j1)
  62.  
  63.                             continue;
  64.  
  65.                         m[i-1][j2] = A[i][j];
  66.  
  67.                         j2++;
  68.  
  69.                     }
  70.  
  71.                 }
  72.  
  73.                 det += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);
  74.  
  75.             }
  76.  
  77.         }
  78.  
  79.         return det;
  80.  
  81.     }
  82.  
  83.  
  84.  
  85.     public static void main(String args[])
  86.  
  87.     {
  88.  
  89.         Scanner input = new Scanner(System.in);
  90.  
  91.         System.out.println("Enter the order of the square matrix");
  92.  
  93.         int n = input.nextInt();
  94.  
  95.  
  96.  
  97.         System.out.println("Enter the elements of the square matrix");
  98.  
  99.         double[][] mat = new double[n][n];
  100.  
  101.         for(int i=0; i<n; i++)
  102.  
  103.         {
  104.  
  105.             for(int j=0; j<n; j++)
  106.  
  107.             {
  108.  
  109.                 mat[i][j] = input.nextDouble();
  110.  
  111.             }
  112.  
  113.         }
  114.  
  115.  
  116.  
  117.         Invertible_Matrix I = new Invertible_Matrix();
  118.  
  119.  
  120.  
  121.         if(I.determinant(mat, n) == 0)
  122.  
  123.         {
  124.  
  125.             System.out.println("Matrix is not Invertible, as the determinant is : "+I.determinant(mat, n));
  126.  
  127.         }
  128.  
  129.         else
  130.  
  131.         {
  132.  
  133.             System.out.println("Matrix is Invertible, as the determinant is : "+I.determinant(mat, n));
  134.  
  135.         }
  136.  
  137.  
  138.  
  139.         input.close();
  140.  
  141.     }
  142.  
  143. }