สมการเชิงเส้นใดๆ ในตัวแปรเดียวมีรูปแบบ aX + b =cX + d ที่นี่จะพบค่าของ X เมื่อให้ค่าของ a, b, c, d
โปรแกรมแก้สมการเชิงเส้นในตัวแปรเดียวมีดังนี้ −
ตัวอย่าง
#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;
}