Solve Determinant of N x N Matrix in Java

This is a Java program which can be used to find the Determinant of any Matrix. This program involves transforming the given N x N matrix to a Upper Triangle form.

Screenshot[Demo]


The Code

package dProBuk;

import java.util.Scanner;

public class Det {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner s= new Scanner(System.in);
  int n;
  System.out.println("Give the value of N for [N x N]: ");
  n=s.nextInt();
  double a[][]=new double [n][n];
  //Storing Values in the Array !
  for (int i=0;i<n;i++)
   for (int j=0;j<n;j++)
    a[i][j]=s.nextInt();
  // Conversion of matrix to upper triangular 
     for(int i = 0; i < n; i++)
     {
         for(int j = 0; j < n; j++)
             if(j>i)
             {
                 double ratio = a[j][i]/a[i][i];
                 for(int k = 0; k < n; k++)
                     a[j][k] -= ratio * a[i][k];                      
             }
     }
     double det = 1.0; //storage for determinant
     for(int i = 0; i < n; i++)
         det *= a[i][i];
     System.out.println("The Det is : "+det);
     s.close();
 }
}


Found Bugs, or wanna Give Hugs, Share it.. and comment them ! :D

Comments

  1. Can u please let me know
    for(int j = 0; j < n; j++)
    if(j>i)
    {
    double ratio = a[j][i]/a[i][i];
    for(int k = 0; k < n; k++)
    a[j][k] -= ratio * a[i][k];
    }

    How it is working.
    In detail.

    I am not able to understand, how ratio working here.

    ReplyDelete

Post a Comment

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java