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:
Java Program to Implement Shunting Yard Algorithm
4 tính chất của lập trình hướng đối tượng trong Java
Spring Autowiring of Generic Types
Java Program to Perform the Unique Factorization of a Given Number
Java toString() Method
A Guide to Apache Commons Collections CollectionUtils
Spring Data Java 8 Support
Lớp HashMap trong Java
TreeSet và sử dụng Comparable, Comparator trong java
New Features in Java 11
An Example of Load Balancing with Zuul and Eureka
Hướng dẫn Java Design Pattern – Singleton
Spring Security Custom AuthenticationFailureHandler
Biểu thức Lambda trong Java 8 – Lambda Expressions
Converting String to Stream of chars
Java Program to Implement Double Order Traversal of a Binary Tree
Collect a Java Stream to an Immutable Collection
StringBuilder vs StringBuffer in Java
A Guide to Queries in Spring Data MongoDB
Spring Cloud AWS – Messaging Support
Spring MVC Custom Validation
Java Program to Find the Minimum value of Binary Search Tree
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement RenderingHints API
Static Content in Spring WebFlux
Java Program to Implement ArrayList API
The Spring @Controller and @RestController Annotations
Hướng dẫn Java Design Pattern – Iterator
Different Ways to Capture Java Heap Dumps
Spring REST with a Zuul Proxy
Luồng Daemon (Daemon Thread) trong Java
Java Program to Check Multiplicability of Two Matrices