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

This is a C++ Program that Solves any Linear Equation in One Variable.

Problem Description

The Problem states that given a linear equation in one variable of the form aX + b = cX + d where a,b,c,d are provided as input, we need to determine the appropriate value of X.

Problem Solution

Solving the equation aX + b = cX + d, we get
=> aX – cX = d – b
=> X = (d-b) / (a-c)
So we can solve the equation by applying the above formula. But, we need to figure out following 2 cases of inputs:
Case-1:

    Now if a==c but b!=d, the equation will reduce to:
    X = (d-b)/0
    Hence this value is invalid.

Case-2:

    Now if a==c but b==d, the equation will reduce to:
    aX = cX
    => X = X (Since a=c)
    Hence, any value of X will satisfy the equation and we get infinite number of solutions.
Expected Input and Output

Case-1:

advertisement
advertisement
    a=2,b=4,c=8,d=16
    X=-2

Case-2:

    a=2,b=5,c=2,d=5
    Infinite Solutions

Case-3:

    a=2,b=5,c=2,d=3
    Wrong Equation: No Solution
Program/Source Code

Here is source code of the C++ Program to Solve any Linear Equation in One Variable. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1.     #include <bits/stdc++.h>
  2.     using namespace std;
  3.     void solve(float a, float b, float c, float d)
  4.     {
  5.         if(a==c && b==d)
  6.             cout<<"Infinite Solutions"<<endl;
  7.         else if(a==c)
  8.             cout<<"Wrong Equation: No Solution"<<endl;
  9.         else
  10.         {
  11.             float X = (d-b)/(a-c);
  12.             cout<<"Solution is X = "<<X<<endl;
  13.         }
  14.     }
  15.     int main()
  16.     {
  17.         float a, b, c, d;
  18.         cout<<"For a linear equation in one variable of the form aX + b = cX + d"<<endl;
  19.  
  20.         cout<<"Enter the value of a : ";
  21.         cin>>a;
  22.  
  23.         cout<<"Enter the value of b : ";
  24.         cin>>b;
  25.  
  26.         cout<<"Enter the value of c : ";
  27.         cin>>c;
  28.  
  29.         cout<<"Enter the value of d : ";
  30.         cin>>d;
  31.  
  32.         solve(a, b, c, d);
  33.     }
Program Explanation

main() – In this function, we take the inputs of a,b,c,d and we compute the result inside the function solve().
solve() – Inside this, we check for the cases defined above and we provide appropriate solution.

Runtime Test Cases
Case-1:
$ g++ Variable_Linear_Eq.cpp
$ ./a.out
For a linear equation in one variable of the form aX + b = cX + d
Enter the value of a : 2
Enter the value of b : 4
Enter the value of c : 4
Enter the value of d : 8
Solution is X = -2
 
Case-2:
$ ./a.out
For a linear equation in one variable of the form aX + b = cX + d
Enter the value of a : 2
Enter the value of b : 5
Enter the value of c : 2
Enter the value of d : 5
Infinite Solutions  
 
Case-3:
$ ./a.out
For a linear equation in one variable of the form aX + b = cX + d
Enter the value of a : 2
Enter the value of b : 5
Enter the value of c : 2
Enter the value of d : 3
Wrong Equation: No Solution

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.