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;
}
广告
数据结构
联网
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP