-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCramerRuleEqSolver.java
More file actions
64 lines (57 loc) · 2.46 KB
/
CramerRuleEqSolver.java
File metadata and controls
64 lines (57 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Rahim Siddiq
Apr 13 2023
Solve 2x2 linear equations
*/
import java.util.Scanner;
public class CramerRuleEqSolver
{
public static void main(String[] args)
{
// Program variables
double a=0, b=0, c=0, d=0, e=0, f=0;
double x=0, y=0;
double determinant=0;
// Title of program and description of program function to user
System.out.println(" ====================================================================");
System.out.println(" ====================== SOLVE LINEAR EQUATIONS ======================");
System.out.println(" ====================================================================");
System.out.println(" Program uses Cramer's rule to solve equations with provided input");
System.out.println(" --------------------------------------------------------------------");
System.out.println();
Scanner input = new Scanner(System.in);
System.out.println(" Please enter a value for variables a, b, c, d, e, f");
System.out.println();
System.out.print(" Please enter a number for a, then press [Enter]: ");
a = input.nextDouble();
System.out.print(" Please enter a number for b, then press [Enter]: ");
b = input.nextDouble();
System.out.print(" Please enter a number for c, then press [Enter]: ");
c = input.nextDouble();
System.out.print(" Please enter a number for d, then press [Enter]: ");
d = input.nextDouble();
System.out.print(" Please enter a number for e, then press [Enter]: ");
e = input.nextDouble();
System.out.print(" Please enter a number for f, then press [Enter]: ");
f = input.nextDouble();
// Compute the determinant of the coefficients
determinant = a * d - b * c;
// If the determinant is zero, the equation has no solution
if (determinant == 0)
{
System.out.println(" --------------------------------------------------------------------");
System.out.println(" The equation has no solution");
}
else
{
// Compute the values of x and y using Cramer's rule and display result
// Calculations
x = (e * d - b * f) / determinant;
y = (a * f - e * c) / determinant;
// Output
System.out.println(" --------------------------------------------------------------------");
System.out.println(" x is: " + x + " and y is: " + y);
}
input.close();
}
}