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:
The Guide to RestTemplate
Using Optional with Jackson
Ignore Null Fields with Jackson
Đồng bộ hóa các luồng trong Java
Versioning a REST API
Java Program to Perform the Unique Factorization of a Given Number
Guide to DelayQueue
Java Program to Implement Pagoda
Java Program to Represent Graph Using Adjacency List
Java Program for Topological Sorting in Graphs
Java Program to Implement Horner Algorithm
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Implement Segment Tree
Model, ModelMap, and ModelAndView in Spring MVC
Java Program to Implement Gale Shapley Algorithm
Spring Security – security none, filters none, access permitAll
Lớp Collectors trong Java 8
Spring Boot - Quick Start
Jackson Ignore Properties on Marshalling
Spring Boot Application as a Service
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement Hash Tables with Quadratic Probing
How To Serialize and Deserialize Enums with Jackson
Java Program to Implement Heap Sort Using Library Functions
Java 8 Stream API Analogies in Kotlin
Java Program to Implement Borwein Algorithm
Case-Insensitive String Matching in Java
Giới thiệu Google Guice – Binding
How to Remove the Last Character of a String?
Spring Boot - CORS Support
Java Program to Perform Arithmetic Operations on Numbers of Size