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:
Annotation trong Java 8
Updating your Password
Java Program to Perform integer Partition for a Specific Case
Java Program to Implement Skew Heap
A Comparison Between Spring and Spring Boot
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
REST Pagination in Spring
A Guide to the finalize Method in Java
Flattening Nested Collections in Java
Java – Convert File to InputStream
Quick Guide on Loading Initial Data with Spring Boot
Java – Random Long, Float, Integer and Double
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Concurrency Interview Questions and Answers
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Tính kế thừa (Inheritance) trong java
Implementing a Runnable vs Extending a Thread
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Perform Searching Based on Locality of Reference
Java Program to Implement JobStateReasons API
Spring Boot - Flyway Database
A Custom Data Binder in Spring MVC
Từ khóa throw và throws trong Java
Java Copy Constructor
Number Formatting in Java
Spring Security Basic Authentication
Exploring the Spring 5 WebFlux URL Matching
A Guide to JPA with Spring
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Mảng (Array) trong Java
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Implement ArrayDeque API