Laxmi
Cramers rule for solving equations using java
1 Answer(s)      6 years ago
Posted in : Java Magazine

My code:

```import java.util.Scanner.*;
import java.util.*;
public class cramersrule
{

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

public void input()
{

Scanner s=new Scanner(System.in);

System.out.println("enter no of equations 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 co-efficient of equations");
for(int i=0;i<N;i++)
{
System.out.println("enter the co-efficient of  of equation" + (i+1));
for(int j=0;j<N;j++)
{
double k=s.nextDouble();
A[i][j]=k;
}
}

B = new double[N];
System.out.println("enter the RHS of equations");
for(int i=0;i<N;i++)
{
System.out.println("enter the RHS of equations" + (i+1));
double k = s.nextDouble();
B[i] = 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 double[] cramers(double A[][],double B[])
{
double temp[][] = new double[N][N];
double x[] = new double[N];
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
if(k == i)
temp[j][k] = B[j];
else
temp[j][k] = A[j][k];
}}
x[i]=determinant(temp,N)/determinant(A,N);

}
for(int i=0;i<N;i++){
System.out.println(x[i]);}
return x;
}

public static void main(String args[]){
double res;

cramersrule d = new cramersrule();
d.input();
d.cramers(d.A,d.B);

}
}
```

Thanks

April 20, 2011 at 12:04 PM

```import java.util.*;

public class cramersrule {
double A[][];
double m[][];
int N;
double B[];

public void input() {
Scanner s=new Scanner(System.in);
System.out.println("enter no of equations 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 co-efficient of equations");
for(int i=0;i<N;i++) {
System.out.println("enter the co-efficient of of equation" + (i+1));
for(int j=0;j<N;j++) {
double k=s.nextDouble();
A[i][j]=k;
}
}
B = new double[N];
System.out.println("enter the RHS of equations");
for(int i=0;i<N;i++){
System.out.println("enter the RHS of equations" + (i+1));
double k = s.nextDouble();
B[i] = 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 double[] cramers(double A[][],double B[]) {
double temp[][] = new double[N][N];
double x[] = new double[N];
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
if(k == i)
temp[j][k] = B[j];
else temp[j][k] = A[j][k];
}
}
x[i]=determinant(temp,N)/determinant(A,N);
}
for(int i=0;i<N;i++){
System.out.println(x[i]);
}
return x;
}
public static void main(String args[]){
double res;
cramersrule d = new cramersrule();
d.input();
d.cramers(d.A,d.B);
}
}
```

Related Tutorials/Questions & Answers:
Cramers rule for solving equations using java
Cramers rule for solving equations using java  My code: import... s=new Scanner(System.in); System.out.println("enter no of equations of matrix... double[N]; } System.out.println("enter the co-efficient of equations
gauss elimination of system of linear equations using java code
gauss elimination of system of linear equations using java code  Here...); System.out.println("enter no of equations "); N = s.nextInt...]; } System.out.println("enter the co-efficient of equations
ILOG Business Rule Studio
-editing and debugging of Java code and rules. Rule Studio supports... ILOG Business Rule Studio       ILOG JRules Rule Studio is an Eclipse-based
Plotting equations on a cartesian Plane
Plotting equations on a cartesian Plane  Hey, Im sort of a beginner of java, and i at the moment of working on a program that takes the input of an equation (eg. y=3x) and plots possible values for it on a cartesian plane thats
help me in solving this error
help me in solving this error  Hello, i have installed tomcat 6.0, i am getting following error while running application...()); %> java file import java.io.IOException; import java.io.PrintWriter
Solving java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean issue
Solving java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean issue  Solving java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean issue In my Spring
java programming - Java Beginners
java programming  asking for java code, solving three unknowns in three equations..I need the thx ahed
Horizontal Rule Attribute in HTML
Horizontal Rule Attribute in HTML        We already know with Horizontal Rule in HTML page. The Horizontal Rule attribute is same as horizontal rule, but only
Flex Rule Control
Rule in Flex:- What is Rule in flex?. The answer is that if user want to create a line between two controls by using Rule control in flex. It is two types... HRule by using <mx:HRule>tag. You can also set the property for these Rule
Horizontal Rule in HTML
Horizontal Rule in HTML       A horizontal Rule in HTML is used to separate two lines or two paragraph between any two objects in HTML.To apply horizontal rule, the <hr>
java programming - Java Beginners
java programming  asking for the java code for solving mathematical equation with two unknown .thnx ahead..  Hi Friend, Please clarify your question. Which mathematical equations you want to solve? Thanks
Solving task 1 by 1 from bundle of task data
Solving task 1 by 1 from bundle of task data  Hello and namaste guys, i really need help here.i use ResultSet to query data from database. as we know, it query row by row using while (ResultSet()) this means, i have a lot
OOP Using JAVA - Java Beginners
OOP Using JAVA  OBJECT ORIENTED PROGRAMMING USING JAVA (hope guys u will help me please i need your help,thank you so much) Create a Java program... GUYS YOU WILL HELP ME  Hi friend, Code to help in solving the problem
Java using arrays - Java Beginners
Java using arrays  Write a program to input a possibly integer n(n<=10);followed by n real values into an array a of size n and the program must...;Hi friend, Code to help in solving the problem : import java.io.
using eval in Java
using eval in Java  using eval in Java
write a programm using java
write a programm using java  print the following using java programming
Using throw in java method
Using throw in java method  using throw with method implies what
survey poll using java
survey poll using java  how to make a survey poll using java? i am using netbeans and glassfish
A Java Program by using JSP
A Java Program by using JSP  how to draw lines by using JSP plz show me the solution by using program
RARP program using java
RARP program using java   hai, how to implement the RARP concept using java
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
Image using Java coding
Image using Java coding  Hai, Display image in pdf file using Java coding through Xsl file.. Please help me.. xsl file generate the pdf file
covert the following using java
covert the following using java  how to convert (for eg : 2.89) . this decimal to binary in java
accessing xml using java
accessing xml using java  I need to retrieve some elements in xml file using java   Hi Friend, Please visit the following links: http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net/xml/getting
uSING JAVA API
uSING JAVA API  How to extract Quickbooks online data using Quickbooks api( Rest ApI) write in java code ? 1.register company account 2..register application 3.get apptoken,consumer key,consumer screte 4.how can i
SMS using Java
/viewqa/Java-Beginners/15745-Sms-to-mobile-using-java-application.html ) How...SMS using Java  I am having a concern , where is the main class in SMSClient.java, if this seems silly please let me know How to execute this java
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know.../xmlParsing/java-xml-parsing-using-sax.shtml http://www.roseindia.net/xml/sax...Read XML using Java  Hi All, Good Morning, I have been working
Tutorials