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 Print the Kind of Rotation the AVL Tree is Undergoing
Array to String Conversions
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Introduction to Spring Data MongoDB
Java Program to find the peak element of an array using Binary Search approach
A Guide to Iterator in Java
Lớp Arrarys trong Java (Arrays Utility Class)
Creating Docker Images with Spring Boot
Java – Delete a File
Prevent Cross-Site Scripting (XSS) in a Spring Application
DynamoDB in a Spring Boot Application Using Spring Data
Notify User of Login From New Device or Location
Java Program to implement Bit Set
A Guide to @RepeatedTest in Junit 5
Spring Security Authentication Provider
Java Program to Implement Knight’s Tour Problem
Spring Boot - Interceptor
Quick Guide to the Java StringTokenizer
Java Program to Implement Find all Back Edges in a Graph
Guide to ThreadLocalRandom in Java
Remove HTML tags from a file to extract only the TEXT
Hướng dẫn Java Design Pattern – Abstract Factory
CyclicBarrier in Java
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement SynchronosQueue API
Spring RestTemplate Request/Response Logging
SOAP Web service: Authentication trong JAX-WS
Converting a List to String in Java
Java Program to Implement Expression Tree
Java Program to Implement Binomial Tree
Java Program to Implement Patricia Trie
OAuth2 Remember Me with Refresh Token