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 Perform Uniform Binary Search
Java Program to Implement Binomial Heap
Java Program to Perform Search in a BST
Documenting a Spring REST API Using OpenAPI 3.0
Java Program to Find Minimum Element in an Array using Linear Search
Collect a Java Stream to an Immutable Collection
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement Radix Sort
Adding Shutdown Hooks for JVM Applications
Java Program to Implement Queue
Java Program to Implement the Vigenere Cypher
Receive email using IMAP
Java Program to Implement Heap Sort Using Library Functions
Java program to Implement Tree Set
Guide to System.gc()
Spring MVC Async vs Spring WebFlux
Spring Boot - Bootstrapping
Spring Security Custom AuthenticationFailureHandler
Composition, Aggregation, and Association in Java
Java Program to Check whether Graph is a Bipartite using DFS
Annotation trong Java 8
Spring Boot - Admin Client
Java equals() and hashCode() Contracts
Java InputStream to Byte Array and ByteBuffer
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java – Create a File
Hướng dẫn sử dụng Java Generics
An Intro to Spring Cloud Contract
Hướng dẫn Java Design Pattern – Composite