# C++ Program to Solve any Linear Equation in One Variable

Any linear equation in one variable has the form aX + b = cX + d. Here the value of X is to be found, when the values of a, b, c, d are given.

A program to solve a linear equation in one variable is as follows −

## Example

Live Demo

#include<iostream>
using namespace std;
int main() {
float a, b, c, d, X;
cout<<"The form of the linear equation in one variable is: aX + b = cX + d"<<endl;
cout<<"Enter the values of a, b, c, d : "<<endl;
cin>>a>>b>>c>>d;
cout<<"The equation is "<<a<<"X + "<<b<<" = "<<c<<"X + "<<d<<endl;

if(a==c && b==d)
cout<<"There are infinite solutions possible for this equation"<<endl;
else if(a==c)
cout<<"This is a wrong equation"<<endl;
else {
X = (d-b)/(a-c);
cout<<"The value of X = "<< X <<endl;
}
}

## Output

The output of the above program is as follows

The form of the linear equation in one variable is: aX + b = cX + d
Enter the values of a, b, c, d :
The equation is 5X + 3 = 4X + 9
The value of X = 6

In the above program, first the values of a, b, c and d are input by the user. Then the equation is displayed. This is given below −

cout<<"The form of the linear equation in one variable is: aX + b = cX + d"<<endl;

cout<<"Enter the values of a, b, c, d : "<<endl;
cin>>a>>b>>c>>d;

cout<<"The equation is "<<a<<"X + "<<b<<" = "<<c<<"X + "<<d<<endl;

If a is equal to c and b is equal to d, then there are infinite solutions for the equation. If a is equal to c, then the equation is wrong. Otherwise, the value of X is calculated and printed. This is given below −

if(a==c && b==d)
cout<<"There are infinite solutions possible for this equation"<<endl;
else if(a==c)
cout<<"This is a wrong equation"<<endl;
else {
X = (d-b)/(a-c);
cout<<"The value of X = "<< X <<endl;
}