A linear equation can be solved using Cramer’s rule. Write a program that prompts the user to enter a, b, c, d, e, and f and displays the result. If a*d - b*c is 0 , report that “The equation has no solution.”
Solution:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double a, b, c, d, e, f;
double x, y, condition;
System.out.print("Enter a, b, c, d, e, f:");
a = s.nextDouble();
b = s.nextDouble();
c = s.nextDouble();
d = s.nextDouble();
e = s.nextDouble();
f = s.nextDouble();
condition = a * d - b * c;
if(condition != 0) {
x = (e * d - b * f) / condition;
y = (a * f - e * c) / condition;
System.out.print("x is " + x + " and y is " + y);
} else {
System.out.print("The equation has no solution.");
}
}
No comments:
Post a Comment