
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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; }
- Related Articles
- What is a Linear Equation in one variable?
- What is meant by linear equation with one variable. Give example.
- Secant method to solve non-linear equation\n
- What are linear equations in one variable?
- Solve a linear matrix equation or system of linear scalar equations in Python
- How can we use the SciPy library to solve a Linear equation?
- Which linear function of SciPy is used to solve a banded matrix equation?
- Which linear function of SciPy is used to solve the circulant matrix equation?
- C program to find the solution of linear equation
- What is a linear equation? Solve: $frac{3m}{2}-3=3$.
- Which one is an example for; standard linear equation?
- Which linear function of SciPy is used to solve Hermitian positive-definite banded matrix equation?
- Solve the following linear equation.( frac{x-5}{3}=frac{x-3}{5} ).
- Solve the pair of linear equation: $2x+y=5 ....... ( i)$$3x+2y=8 ....... ( ii)$
- How to solve simultaneous linear equations?

Advertisements