Exercise 3.3 - Algebra: Solve 2x2 Linear Equations

The program below is the solution to Liang's Introduction to Java (9th Edition) Chapter 3 Exercise 3.3.

```/**
*
* @Author: Aghatise Osazuwa
* Website: www.cscprogrammingtutorials.com
*/
import java.util.Scanner;

public class Ex03_03 {
public static void main(String[] args) {
// declare variables
double a, b, c, d, e, f, num1, num2, denom, x, y;

// create Scanner to read user input
Scanner input = new Scanner(System.in);

System.out.print("This program solves 2x3 system of linear equations. "
+ "\n\nFor example in the following equation 9x + 4y = -6 "
+ "\n                                      3x - 5y = -21"
+ "\na, b, c, d, e, f = 9, 4, 3, -5, -6, -21 respectively. "
+ "\nEnter each number separated by pressing space or enter.");

// prompt user to enter details
System.out.print("\n\nEnter a, b, c, d, e, f: ");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
d = input.nextDouble();
e = input.nextDouble();
f = input.nextDouble();

//calculate x and y
num1 = ((e * d) - (b * f));
num2 = ((a * f) - (e * c));
denom = ((a * d) - (b * c));

x = num1 / denom;
y = num2 / denom;

if (denom == 0) {
System.out.println("\nThe equation has no solution.");
} else {
System.out.println("x is " + x + " and y is " + y);
}
}
}
```
 Program sample run

DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.