# Java Program to Solve Any Linear Equations

«
»
This is java program to solve the system of linear equations. This can be done by first representing equations(vectors) to matrix form, then finding the inverse of the matrix formed by the coefficients of variable and multiplying it with constants.

Here is the source code of the Java Program to Solve any Linear Equation in One Variable. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `//This is a sample program to solve the linear equations.`
2. `import java.util.Scanner;`
3. ` `
4. `public class Solve_Linear_Equation `
5. `{`
6. `    public static void main(String args[])`
7. `    {`
8. `        char []var = {'x', 'y', 'z', 'w'};`
9. `        System.out.println("Enter the number of variables in the equations: ");`
10. `        Scanner input = new Scanner(System.in);`
11. `        int n = input.nextInt();`
12. `        System.out.println("Enter the coefficients of each variable for each equations");`
13. `        System.out.println("ax + by + cz + ... = d");`
14. `        double [][]mat = new double[n][n];`
15. `        double [][]constants = new double[n];`
16. `        //input`
17. `        for(int i=0; i<n; i++)`
18. `        {`
19. `            for(int j=0; j<n; j++)`
20. `            {`
21. `                mat[i][j] = input.nextDouble();`
22. `            }`
23. `            constants[i] = input.nextDouble();`
24. `        }`
25. `        //Matrix representation`
26. `        for(int i=0; i<n; i++)`
27. `        {`
28. `            for(int j=0; j<n; j++)`
29. `            {`
30. `                System.out.print(" "+mat[i][j]);`
31. `            }`
32. `            System.out.print("  "+ var[i]);`
33. `            System.out.print("  =  "+ constants[i]);`
34. `            System.out.println();`
35. `        }`
36. `        //inverse of matrix mat[][]`
37. `        double inverted_mat[][] = invert(mat);`
38. `        System.out.println("The inverse is: ");`
39. `        for (int i=0; i<n; ++i) `
40. `        {`
41. `            for (int j=0; j<n; ++j)`
42. `            {`
43. `                System.out.print(inverted_mat[i][j]+"  ");`
44. `            }`
45. `            System.out.println();`
46. `        }`
47. `        //Multiplication of mat inverse and constants`
48. `        double result[][] = new double[n];`
49. `        for (int i = 0; i < n; i++) `
50. `        {`
51. `            for (int j = 0; j < 1; j++) `
52. `            {`
53. `                for (int k = 0; k < n; k++)`
54. `                {	 `
55. `                    result[i][j] = result[i][j] + inverted_mat[i][k] * constants[k][j];`
56. `                }`
57. `            }`
58. `        }`
59. `        System.out.println("The product is:");`
60. `        for(int i=0; i<n; i++)`
61. `        {`
62. `            System.out.println(result[i] + " ");`
63. `        }`
64. `        input.close();`
65. ` `
66. `    }`
67. ` `
68. `    public static double[][] invert(double a[][]) `
69. `    {`
70. `        int n = a.length;`
71. `        double x[][] = new double[n][n];`
72. `        double b[][] = new double[n][n];`
73. `        int index[] = new int[n];`
74. `        for (int i=0; i<n; ++i) `
75. `            b[i][i] = 1;`
76. ` `
77. ` // Transform the matrix into an upper triangle`
78. `        gaussian(a, index);`
79. ` `
80. ` // Update the matrix b[i][j] with the ratios stored`
81. `        for (int i=0; i<n-1; ++i)`
82. `            for (int j=i+1; j<n; ++j)`
83. `                for (int k=0; k<n; ++k)`
84. `                    b[index[j]][k]`
85. `                    	    -= a[index[j]][i]*b[index[i]][k];`
86. ` `
87. ` // Perform backward substitutions`
88. `        for (int i=0; i<n; ++i) `
89. `        {`
90. `            x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];`
91. `            for (int j=n-2; j>=0; --j) `
92. `            {`
93. `                x[j][i] = b[index[j]][i];`
94. `                for (int k=j+1; k<n; ++k) `
95. `                {`
96. `                    x[j][i] -= a[index[j]][k]*x[k][i];`
97. `                }`
98. `                x[j][i] /= a[index[j]][j];`
99. `            }`
100. `        }`
101. `        return x;`
102. `    }`
103. ` `
104. `// Method to carry out the partial-pivoting Gaussian`
105. `// elimination.  Here index[] stores pivoting order.`
106. ` `
107. `    public static void gaussian(double a[][], int index[]) `
108. `    {`
109. `        int n = index.length;`
110. `        double c[] = new double[n];`
111. ` `
112. ` // Initialize the index`
113. `        for (int i=0; i<n; ++i) `
114. `            index[i] = i;`
115. ` `
116. ` // Find the rescaling factors, one from each row`
117. `        for (int i=0; i<n; ++i) `
118. `        {`
119. `            double c1 = 0;`
120. `            for (int j=0; j<n; ++j) `
121. `            {`
122. `                double c0 = Math.abs(a[i][j]);`
123. `                if (c0 > c1) c1 = c0;`
124. `            }`
125. `            c[i] = c1;`
126. `        }`
127. ` `
128. ` // Search the pivoting element from each column`
129. `        int k = 0;`
130. `        for (int j=0; j<n-1; ++j) `
131. `        {`
132. `            double pi1 = 0;`
133. `            for (int i=j; i<n; ++i) `
134. `            {`
135. `                double pi0 = Math.abs(a[index[i]][j]);`
136. `                pi0 /= c[index[i]];`
137. `                if (pi0 > pi1) `
138. `                {`
139. `                    pi1 = pi0;`
140. `                    k = i;`
141. `                }`
142. `            }`
143. ` `
144. `   // Interchange rows according to the pivoting order`
145. `            int itmp = index[j];`
146. `            index[j] = index[k];`
147. `            index[k] = itmp;`
148. `            for (int i=j+1; i<n; ++i) 	`
149. `            {`
150. `                double pj = a[index[i]][j]/a[index[j]][j];`
151. ` `
152. ` // Record pivoting ratios below the diagonal`
153. `                a[index[i]][j] = pj;`
154. ` `
155. ` // Modify other elements accordingly`
156. `                for (int l=j+1; l<n; ++l)`
157. `                    a[index[i]][l] -= pj*a[index[j]][l];`
158. `            }`
159. `        }`
160. `    }`
161. `}`

Output:

```\$ javac Solve_Linear_Equation.java
\$ java Solve_Linear_Equation
Enter the number of variables in the equations:
2
Enter the coefficients of each variable for each equations
ax + by + cz + ... = d
1 2 3
3 2 1

1.0 2.0  x  =  3.0
3.0 2.0  y  =  1.0

The inverse is:
-0.49999999999999994  0.5
0.7499999999999999  -0.24999999999999997

The product is:
-0.9999999999999998
1.9999999999999996```

Sanfoundry Global Education & Learning Series – 1000 Java Programs. 