C ++程序可在一个变量中求解任何线性方程

一个变量中的任何线性方程均具有aX + b = cX + d的形式。当给出a,b,c,d的值时,可以找到X的值。

解决一个变量中的线性方程的程序如下-

示例

#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;
   }
}

输出结果

上面程序的输出如下

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

在上述程序中,首先由用户输入a,b,c和d的值。然后显示方程式。这在下面给出-

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;

如果a等于c且b等于d,则方程有无限解。如果a等于c,则该方程式是错误的。否则,将计算并打印X的值。这在下面给出-

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;
}