দ্বিঘাত সমীকরণ: ax² + bx + c = 0। এর মূলের সূত্র:

বিবিচনক (Discriminant) D = b² - 4ac অনুযায়ী তিনটি কেস থাকে:
D > 0: দুটি ভিন্ন বাস্তব মূলD = 0: দুটি সমান বাস্তব মূলD < 0: দুটি জটিল (complex) মূল
উদাহরণ
#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" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
}
}
return 0;
}
আউটপুট
Roots are real and same. Root 1 = Root 2 = -1
প্রোগ্রামে প্রথমে বিবিচনক (discriminant) ক্যালকুলেট করা হয়। তারপর এর মান অনুযায়ী মূলগুলো নির্ণয় করা হয়। cmath লাইব্রেরির sqrt() ফাংশন বর্গমূল বের করতে ব্যবহৃত হয়েছে।