Thursday 26 March 2020

SOLVING LINEAR EQUATION USING CRAMER'S RULE BY JAVA LANGUAGE

Q-31- You can use Cramer’s rule to solve the following  2 X 2 system of linear equation:

π‘Žπ‘₯ + 𝑏𝑦 = 𝑒     
𝑐π‘₯ + 𝑑𝑦 = 𝑓
                           x=(𝑒𝑑 − 𝑏𝑓) /(π‘Žπ‘‘ − 𝑏𝑐)                  𝑦 =(π‘Žπ‘“ − 𝑒𝑐)/ (π‘Žπ‘‘ − 𝑏𝑐)

Write a java program that prompts the user to enter a, b, c, d, e, and f and displays the result. If (ad - bc) is 0, report that “The equation has no solution.”

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double a ,b ,c ,d ,e ,f ;
double x , y ,z;
System.out.println("Enter The Value Of a ,b ,c ,d ,e ,f :");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
d=sc.nextDouble();
e=sc.nextDouble();
f=sc.nextDouble();
z=a*d-b*c;
x=(e*d-b*f)/z;
y=(a*f-e*c)/z;
if(z==0)
{
System.out.println("The Equation Has No Solution.");
}
else
{
System.out.println("x is "+x+" and y is "+y);
}
}


}

OUTPUT :
Enter The Value Of a ,b ,c ,d ,e ,f :
9.0 4.0 3.0 -5.0 -6.0 -21.0

x is -2.0 and y is 3.0

Enter The Value Of a ,b ,c ,d ,e ,f :
 1.0 2.0 2.0 4.0 4.0 5.0 
The Equation Has No Solution.


No comments:

Post a Comment