This is a java program to solve a linear equation in one variable.
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.
//This is a java program to find the solution to the linear equation in single variable import java.util.Scanner; public class LEquation { public static void main(String args[]) { String eqn = ""; float ans = 0; float coeffSum = 0; float constSum = 0; float coeffx[] = new float[100]; float[] constant = new float[100]; Scanner in = new Scanner(System.in); System.out.println("Enter a linear equation\n"); eqn = in.nextLine(); eqn += "\n"; // System.out.println(eqn); for (int i = 0, j = 0, k = 0; i < eqn.length() - 1;) { if (eqn.charAt(i + 1) == 'x' && i < eqn.indexOf("=")) { if (i != 0 && eqn.charAt(i - 1) == '-') { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = -(Integer.parseInt(x, 10)); coeffx[j++] = n; } } else { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = Integer.parseInt(x, 10); coeffx[j++] = n; } } i += 3; } if (eqn.charAt(i + 1) == 'x' && i > eqn.indexOf("=")) { if (eqn.charAt(i - 1) == '-') { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = Integer.parseInt(x, 10); coeffx[j++] = n; } } else { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = -(Integer.parseInt(x, 10)); coeffx[j++] = n; } } i += 3; } if (eqn.charAt(i + 1) != 'x' && i < eqn.indexOf("=")) { if (eqn.charAt(i - 1) == '-') { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = -(Integer.parseInt(x, 10)); constant[k++] = n; } } else { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = Integer.parseInt(x, 10); constant[k++] = n; } } i += 2; } if (eqn.charAt(i + 1) != 'x' && i > eqn.indexOf("=")) { if (eqn.charAt(i - 1) == '-') { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = Integer.parseInt(x, 10); constant[k++] = n; } } else { String x = eqn.substring(i, i + 1); if (x != "+" && x != "-") { int n = -(Integer.parseInt(x, 10)); constant[k++] = n; } } i += 2; } } for (int i = 0; i < coeffx.length; i++) coeffSum += coeffx[i]; for (int i = 0; i < constant.length; i++) constSum += constant[i]; ans = constSum / coeffSum; System.out.println("Value of x = " + (-ans)); in.close(); } }
Output:
$ javac LEquation.java $ java LEquation Enter a linear equation 2x+5=4x+9 Value of x = -2.0
Related posts:
Jackson – Bidirectional Relationships
Java Program to Find Whether a Path Exists Between 2 Given Nodes
HttpClient 4 – Follow Redirects for POST
Java Program to Implement Bloom Filter
Simplify the DAO with Spring and Java Generics
OAuth2 Remember Me with Refresh Token
How To Serialize and Deserialize Enums with Jackson
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Comparing Two HashMaps in Java
Inheritance with Jackson
Java Program to implement Priority Queue
How to Return 404 with Spring WebFlux
Hướng dẫn Java Design Pattern – Factory Method
Object Type Casting in Java
Hướng dẫn Java Design Pattern – Interpreter
Entity To DTO Conversion for a Spring REST API
Biến trong java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Program to Implement LinkedBlockingDeque API
Spring Data JPA Delete and Relationships
Test a REST API with Java
Database Migrations with Flyway
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Introduction to Java Serialization
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Generate N Number of Passwords of Length M Each
Control the Session with Spring Security
Guide to the Fork/Join Framework in Java
How to Get the Last Element of a Stream in Java?
Spring Boot - OAuth2 with JWT
Java Program to Implement Ternary Search Algorithm