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:
Spring Boot - Enabling HTTPS
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Command-Line Arguments in Java
Guava – Join and Split Collections
Converting between an Array and a List in Java
Guide to WeakHashMap in Java
A Guide to Iterator in Java
Receive email using POP3
Java Program to Implement CopyOnWriteArrayList API
Testing an OAuth Secured API with Spring MVC
Introduction to the Java ArrayDeque
Spring @RequestParam Annotation
Spring Boot - Hystrix
Tính đóng gói (Encapsulation) trong java
Introduction to Project Reactor Bus
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
SOAP Web service: Authentication trong JAX-WS
Optional trong Java 8
Spring Boot - OAuth2 with JWT
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Finding Max/Min of a List or Collection
Java Program to Perform String Matching Using String Library
Removing all duplicates from a List in Java
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Implement Warshall Algorithm
Hướng dẫn Java Design Pattern – Command
Spring Boot - Tomcat Port Number
Java Program to Implement PriorityQueue API
Java Program to Describe the Representation of Graph using Adjacency List
CyclicBarrier in Java