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:
Giới thiệu Google Guice – Dependency injection (DI) framework
A Guide to ConcurrentMap
Java Copy Constructor
Spring Boot - Logging
Receive email using POP3
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java – InputStream to Reader
Spring WebClient and OAuth2 Support
Java Program to Perform Polygon Containment Test
Java Program to Implement CopyOnWriteArrayList API
The Registration Process With Spring Security
A Guide to the Java ExecutorService
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Java TreeMap vs HashMap
Spring REST with a Zuul Proxy
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Find the GCD and LCM of two Numbers
Working with Kotlin and JPA
Hướng dẫn Java Design Pattern – Dependency Injection
Xây dựng ứng dụng Client-Server với Socket trong Java
Tìm hiểu về Web Service
Spring Boot - Tomcat Deployment
Java – Rename or Move a File
Java Program to Implement Treap
Bootstrap a Web Application with Spring 5
Java Program to Represent Graph Using Adjacency List
Introduction to Spring Data JDBC
Java Program to Implement Queue
Query Entities by Dates and Times with Spring Data JPA
Spring REST API with Protocol Buffers
Filtering and Transforming Collections in Guava
Java Program to Implement Double Order Traversal of a Binary Tree