Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Magazine Cramers rule for solving equations using java

Have Programming Question? Ask it here!
 
 
 


Laxmi
Cramers rule for solving equations using java
1 Answer(s)      3 years and 3 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


Advertisement
View Answers

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
Advertisements
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
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
using java files - Java Beginners
using java files  how i use files in java to store as eg. (information about movies as its name) & when user ask about some movie the movie will come with a picture associated with it plz write me a couple of line codes
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java? whats the java program or code to that. Thank you rose india and java experts  Hi Friend, Try the following code: import java.util.
JSP using java - Java Beginners
JSP using java  sir, In my jsp page contains 3 hyperlinks say (link1 link2 link3).I want to write some java code using servlet for each hyperlinks.how to forward JSP page to servlet.I m using 3 hyperlinks in my page.My
java code using while loop
java code using while loop  
Java Programming using array
Java Programming using array  I'm creating a programming using array that will accept 10 letters and test if those letters are consonant...','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'}; char ch
java using JSP - Java Beginners
java using JSP  hi...i has been created one JSp page.in this page have one links name as "Show datas in Excel format".I want to create Excel file... using postgres DB.This DB have more than 4 Rows and Columns.  Hi Friend
java in using brausing
java in using brausing  sir, i am beginner java developer ,sir i am creat's the image viewer Jframe but not a created frame plz. halpe me give the image viewer Jframe code   import java.io.*; import java.awt.
OOP using Java - Java Beginners
OOP using Java  Can you write a Java statement that creates the object mysteryClock of the Clock type, and initialize the instance variables hr,min...); } } For more information on Java visit to : http://www.roseindia.net/java
How to design a form using java?
How to design a form using java?  Please help in designing of form using java for my project
java code for PartialSearch using Map????
java code for PartialSearch using Map????  java code for Partial Search using Map
how to update values of a html form into an excel table using java servlets?
how to update values of a html form into an excel table using java servlets?  i have written a java servlet program, which has a html form... are not loading into excel table. would you help me in solving this problem
how to load values of html form into an excel table using java servlet?
how to load values of html form into an excel table using java servlet?   i have written a java servlet program, which has a html form to be filled... are not loading into excel table. would you help me in solving this problem
Video streaming using java
Video streaming using java  I want to create a website based on video streaming.The user has to pay and watch the videos stored in the server.So we have to stream the video in the server side and send that to the client side
Java using Jsp - Java Beginners
Java using Jsp  hi sir, again i m getting error like. HSSFWorkbook cannot be resolved to a type. HSSFSheet cannot be resolved to a type. HSSFRow cannot be resolved to a type I included Maximum all jar file in eclipse
Using swing - Java Beginners
Using swing  How can one use inheritance and polymophism while developing GUI of this question. Develop an application that allows a student to open an account. The account may be a savings account or a current account
JSP using java - Hibernate
JSP using java  This is my part of Excal sheet code using jdbc connection. int index=1; while(rs.next... using hibernate.I am having function called displayIps() and displayvalues
Matrix Addition using RMI in Java
Matrix Addition using RMI in Java  Source Code for Matrix Addition using RMI in Java
Without Using Form in Java Script
Without Using Form in Java Script  HOw to Reset the data in javascript without using form tag
Using a JFileChooser - Java Beginners
Using a JFileChooser  Dear Editor, How to select an image from JFileChooser & add the image in mySQL5 database? Thanks in advanced. Regards, Melvin.  Hi Friend, Try the following code: import java.sql.
JSP using java - Java Beginners
JSP using java  hi, In my jsppage has Combo box(contains inpacks,outpacks,system uptime etc) component and options like (bar chart,pie chart etc). I have core java code for (inpacks,outpacks,system uptime etc
 

 

 

DMCA.com