In: Computer Science
Use Java
(Algebra: solve 2 x 2 linear equations)
A linear equation can be solved using Cramer’s rule given in
Programming Exercise 1.13.
Write a program that prompts the user to enter a, b, c, d, e, and f
and displays the result.
If ad - bc is 0, report The equation has no solution.
Sample Run 1
Enter 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
Sample Run 2
Enter a, b, c, d, e, f: 1.0 2.0 2.0 4.0 4.0 5.0
The equation has no solution
Sample Run 3
Enter a, b, c, d, e, f: 1.0 2.0 2.0 4.0 4.0 5.0 6.0
x is -4.0 and y is 4.5
Class Name: Exercise03_03
If you get a logical or runtime error, please refer
https://liveexample.pearsoncmg.com/faq.html.
Program:
import java.util.*; // import packages
import java.io.*;
import java.math.*;
public class Exercise03_03 // class name
{
static double a,b,c,d,e,f,x,y,m; //
variable declaration
public static void main(String[] args) //
main function
{
Scanner sc=new
Scanner(System.in); // object for scanner class
System.out.println("\nEnter
a,b,c,d,e,f values");
a=sc.nextDouble();
// reading input values
b=sc.nextDouble();
// reading input values
c=sc.nextDouble();
// reading input values
d=sc.nextDouble();
// reading input values
e=sc.nextDouble();
// reading input values
f=sc.nextDouble();
// reading input values
m=(a*e-b*d); //
calculate mod
if(m==0) // check mod
value is Zero
System.out.println("No
Solution"); // if zero No solution
else
// otherwise
{
x=(c*e-f*b)/m; // calculating x
y=(a*f-c*d)/m; // calculating Y
System.out.println("x is"+Math.round(x)); // DISPLAY
X
System.out.println("y is"+Math.round(y)); // DISPLAY
Y
}
}
}
OUTPUT:
Program Screen Shot:
Note:
Program Name: Exercise03_03.java
The above program is done by using jdk1.7 compiler