Share on Google+Share on Google+

Laxmi
determinant of n*n matrix using java code
1 Answer(s)      6 years ago
Posted in : Java Magazine

Here is my code:


import java.util.Scanner.*;
import java.util.*;
public class determinantcode
{

double A[][];
double m[][];
int N;



public input()
{
Scanner s=new Scanner(System.in);
System.out.println("enter dimension of matrix");
N=s.nextInt();
a = new double[N][];
for(int i=0;i<N;i++)
{
A[i]=new double[n];
}

System.out.println("enter the elements of matrix");
for(int i=0;i<N;i++)
{
System.out.println("enter the elements of matrix of equation" + (i+1));
for(int j=0;j<N;j++)
{
int k=s.nextDouble();
A[i][j]=k;
}
}

}
public double determinant(double A[][],int N)
{


double det=0;
double res;


if(N == 1)
res = A[0][0];

else if (N == 2)
{
res = A[0][0]*A[1][1] - A[1][0]*A[0][1];
}

else
{
res=0;
for(int j1=0;j1<N;j1++)
{
m = new double[N-1][];
 for(int k=0;k<(N-1);k++)
   m[k] = new double[N-1];
for(int i=1;i<N;i++)
{
int j2=0;
for(int j=0;j<N;j++)
{
  if(j == j1)
   continue;
  m[i-1][j2] = A[i][j];
  j2++;
}
}
 res += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);


}
}


return res;

}

public static void main(String args[]){
double res;
 determinantcode d = new determinantcode();
  d.input();
  res = d.determinant(d.A,d.N);
  System.out.println("the determinant valaue is  " + res);
 }


}

Thanks

Ads

View Answers

April 20, 2011 at 2:30 PM


import java.util.Scanner.*;
import java.util.*;
public class determinantcode
{

double A[][];
double m[][];
int N;

public void input(){
Scanner s=new Scanner(System.in);
System.out.println("enter dimension of matrix");
N=s.nextInt();
A = new double[N][];
for(int i=0;i<N;i++)
{
A[i]=new double[N];
}

System.out.println("enter the elements of matrix");
for(int i=0;i<N;i++)
{
System.out.println("enter the elements of matrix of equation" + (i+1));
for(int j=0;j<N;j++)
{
int k=s.nextInt();
A[i][j]=k;
}
}
}
public double determinant(double A[][],int N){
double det=0;
double res;
if(N == 1)
res = A[0][0];
else if (N == 2){
res = A[0][0]*A[1][1] - A[1][0]*A[0][1];
}
else{
res=0;
for(int j1=0;j1<N;j1++){
m = new double[N-1][];
 for(int k=0;k<(N-1);k++)
   m[k] = new double[N-1];
for(int i=1;i<N;i++){
int j2=0;
for(int j=0;j<N;j++){
  if(j == j1)
   continue;
  m[i-1][j2] = A[i][j];
  j2++;
}
}
 res += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);
}
}
return res;
}
public static void main(String args[]){
double res;
 determinantcode d = new determinantcode();
  d.input();
  res = d.determinant(d.A,d.N);
  System.out.println("the determinant valaue is  " + res);
 }
}









Tutorials   
Java Spring Hibernate Struts Training Default value of path variable in Windows 7 Windows 7 refresh DNS 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1) how to change a column data in a every row in a text file in java cursor location what is the Advantages moqui framework vs other frameworks what is the difference between moqui framework vs other frameworks Develop Bulk SMS sender Bundle of servers interface ALU Computer Term How to find the size of a dynamic webpage using java... eg : Youtube........ Question in Create Arraylist ? active window means 1024 bytes equals to can a volatile memory loss data on power cut What is E-Learning? What is E-Learning? What is hacker? different types of storage devices of a computer Examples of optical disk Main job of cpu Main memory is also known as Define computer software. connection between your computer and local isp which part of the cpu does math calculations What is procedure Where i can use UNIX Operating System? What was ENIAC One byte consists of how many bits? What is the use of Bluetooth technology basic function of computer system how to measure data transfer speed GIGO Full Form What is parallel port where are program instructions and data values stored Nible is a computer network is referred as The input device used by computer is The product of data processing is The CPU(Central processing unit)consist of One-thousand of a second is called Alternate facilities of programs, data files, hardware equipment, etc. One-Millionth of a second is referred as Storage medium that loses its contents A LSI chip contains the entire CPU of computer except main memory is called ascending order of a data hierarchy Combining records from two or more orders files into a single ordered file is called Computer questions for Bank PO Exams and upsc competitive exams characteristics of a modem computer

Ads

 
Advertisement null

Ads