0% found this document useful (0 votes)
2 views1 page

Simple Quadratic

This C++ program calculates the roots of a quadratic equation using the quadratic formula. It includes a custom function to compute the square root through repeated subtraction. The program handles cases for no real roots, equal roots, and distinct real roots based on the discriminant value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Simple Quadratic

This C++ program calculates the roots of a quadratic equation using the quadratic formula. It includes a custom function to compute the square root through repeated subtraction. The program handles cases for no real roots, equal roots, and distinct real roots based on the discriminant value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

// Simple function to calculate square root using repeated subtraction


double simpleSqrt(double number) {
if (number < 0) return 0;
if (number == 0) return 0;

double result = number;


double precision = 0.00001;
while ((result * result - number) > precision) {
result = (result + number / result) / 2;
}
return result;
}

int main() {
double a, b, c;

cout << "Enter a: ";


cin >> a;
cout << "Enter b: ";
cin >> b;
cout << "Enter c: ";
cin >> c;

if (a == 0) {
cout << "Divided by zero" << endl;
return 0;
}

double discriminant = b * b - 4 * a * c;

if (discriminant < 0) {
cout << "No real roots" << endl;
}
else if (discriminant == 0) {
cout << "Equal roots" << endl;
double x = -b / (2 * a);
cout << "x = " << x << endl;
}
else {
cout << "Real roots" << endl;
double sqrtDiscriminant = simpleSqrt(discriminant);
double x1 = (-b + sqrtDiscriminant) / (2 * a);
double x2 = (-b - sqrtDiscriminant) / (2 * a);
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

return 0;
}

You might also like