একটি দ্বিঘাত সমীকরণ ax 2 আকারে রয়েছে + bx + c. দ্বিঘাত সমীকরণের মূলগুলি নিম্নলিখিত সূত্র −
দ্বারা দেওয়া হয়

তিনটি ক্ষেত্রে আছে -
b 2 <4*a*c - শিকড় বাস্তব নয় অর্থাৎ তারা জটিল
b 2 =4*a*c - শিকড় বাস্তব এবং উভয় মূল একই।
b 2 > 4*a*c - শিকড় বাস্তব এবং উভয় মূল ভিন্ন
একটি দ্বিঘাত সমীকরণের মূল খুঁজে বের করার প্রোগ্রামটি নিম্নরূপ দেওয়া হল।
উদাহরণ
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int a = 1, b = 2, c = 1;
float discriminant, realPart, imaginaryPart, x1, x2;
if (a == 0) {
cout << "This is not a quadratic equation";
}else {
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
} else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "Root 1 = Root 2 =" << x1 << endl;
}else {
realPart = (float) -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" <<end;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" <<end;
}
}
return 0;
} আউটপুট
Roots are real and same. Root 1 = Root 2 =-1
উপরের প্রোগ্রামে, প্রথমে বৈষম্যকারী গণনা করা হয়। যদি এটি 0-এর বেশি হয়, তাহলে উভয় মূলই বাস্তব এবং ভিন্ন।
এটি নিম্নলিখিত কোড স্নিপেট দ্বারা প্রদর্শিত হয়৷
৷if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
} যদি বৈষম্য 0 এর সমান হয়, তাহলে উভয় মূলই বাস্তব এবং একই। এটি নিম্নলিখিত কোড স্নিপেট দ্বারা প্রদর্শিত হয়৷
৷else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "Root 1 = Root 2 =" << x1 << endl;
} যদি বৈষম্যকারী 0-এর কম হয়, তাহলে উভয় মূলই জটিল এবং ভিন্ন। এটি নিম্নলিখিত কোড স্নিপেট দ্বারা প্রদর্শিত হয়৷
৷else {
realPart = (float) -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
}