- 1

Write and test a program that solves quadratic equations. A quadratic equation is an equation of the form ax2+ bx + c =0 ,where a,b and c are given coefficient and x is the unknown The coefficients are real number inputs ,so they should be declared of type float or double Since quadratic equations typically have two solutions ,use x1 and x2 for the solutions to be output These should be declared of type double to avoid inaccuracies from round-off error

28th Jun 2016, 7:19 PM
Ogunjirin Emmanuel
Ogunjirin Emmanuel - avatar
3 Réponses
+ 1
#include <iostream> #include<cmath> using namespace std; int main(){ int a, b, c; float x1, x2; cin >> a; cin >> b; cin >> c; if(a == 0){ x1 = x2 = - (c/b); cout << x1 << endl << x2 << endl; } else if(b * b - 4 * a * c < 0) cout << "No real solution"; else{ x1 = (-b + (sqrt(b*b - 4*a*c))) / (2 * a); x2 = (-b - (sqrt(b*b - 4*a*c))) / (2 * a); cout << x1 << endl << x2 << endl; } return 0; }
8th Jul 2016, 1:22 AM
Tony Christopher
Tony Christopher - avatar
0
Hi Ogunjirin! Even though I don't give you a coded solution, I will still give you pointers of how to solve this on your own. Finding the root of a function is a classic function optimization problem. The most simple and commonly used algorithm to solve a function optimization problem is the gradient descent algorithm. Gradient descent finds a *minimum* of a function (not necessarily a root). Nevertheless, after you researched gradient descent, you can turn the above mentioned polynomial into a similar function that has its minima exactly where the above mentioned function has its roots. You just have to get the absolute value of the function (as all negative numbers will become positive and all positives are greater than 0). Hope this helps.
29th Jun 2016, 1:50 AM
Stefan
Stefan - avatar
0
What is the program that solve the quadratic equation using c program
10th Nov 2023, 3:48 PM
Ayuba Lawali Tela
Ayuba Lawali Tela - avatar