# Cramers rule for solving equations using java

Laxmi
Cramers rule for solving equations using java
1 Answer(s)      6 years and 4 months 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);
}
}
```

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
-editing and debugging of Java code and rules. Rule Studio supports... ILOG Business Rule Studio       ILOG JRules Rule Studio is an Eclipse-based
java programming - Java Beginners
java programming  asking for java code, solving three unknowns in three equations..I need the thx ahed
Version of com.kasonchan>equations-massbalance_2.11 dependency
List of Version of com.kasonchan>equations-massbalance_2.11 dependency
Version of com.kasonchan>equations-monooperation_2.11 dependency
List of Version of com.kasonchan>equations-monooperation_2.11 dependency
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
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
Maven Dependency equations-massbalance_2.11 >> 0.1.0
Maven Dependency equations-massbalance_2.11 >> 0.1.1
Maven Dependency equations-monooperation_2.11 >> 0.1.0
Maven Dependency equations-monooperation_2.11 >> 0.1.1
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
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
using eval in Java
using eval in Java  using eval in Java
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
Using throw in java method
Using throw in java method  using throw with method implies what
write a programm using java
write a programm using java  print the following using java programming
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
survey poll using java
survey poll using java  how to make a survey poll using java? i am using netbeans and glassfish
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.
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
RARP program using java
RARP program using java   hai, how to implement the RARP concept using java
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java