Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++

A simple program to solve quadratic equations with

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
8 Nov 2010CPOL 38.4K   3  
Simple and prints imaginary roots too!float a,b,c,x1,x2,d,dsq;printf("ax^2 + bx + c = 0");printf("\nEnter a,b,c separated by commas : \n");scanf("%f,%f,%f",&a,&b,&c);d = b*b-(4*a*c);if(d>=0){dsq=sqrt(d);x1 = (-b+dsq)/(2*a);x2 = (-b-(dsq))/(2*a);printf("\nRoot 1 : %f\nRoot 2...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
16 Nov 2010Alain Rist
I prefer this :) #include #include #include #include static const double bad_double = std::numeric_limits::quiet_NaN();class QuadSolver{ static bool IsZero(double val) { return (val == 0) || (fabs(val) <...
Please Sign up or sign in to vote.
9 Nov 2010YvesDaoust
// Real solutions of the quadratic equation A x^2 + B x + C = 0// Returns two roots (possibly identical) in increasing order, or nonebool Quadratic(double A, double B, double C, double R[2]){ if (A == 0) { // Linear, impossible or degenerate, cannot find two roots ...
Please Sign up or sign in to vote.
10 Nov 2010federico.strati
The correct way to solve quadratic equations is none of the above. For the correct algorithm as applied in the following function, seenumerical recipes in C and the Wolfram site at http://mathworld.wolfram.com/QuadraticEquation.html#include bool quadratic(double a, double b,...
Please Sign up or sign in to vote.
20 May 2010ReymonARG
#include #include #include "Raiz_Cuadratica.h"int main(){ // float xx = 1; float x = 1; float i = -12; RaizCuadratica *lala = new RaizCuadratica( xx, x, i ); RaizCuadratica::sRaiz raiz = lala->GetRaiz(); printf( "x1 = %f || x2 = %f\n\n",...
Please Sign up or sign in to vote.
7 Nov 2010TomSeaton
#include<stdio...
Please Sign up or sign in to vote.
11 Nov 2010Software_Developer 7 alternatives  
A simple program to calculate quadratic equation with.Input MUST have the format:AX2 + BX + C = 0EXAMPLE: input the equation 2X2 + 4X -30 = 0 as:A= 2 B= 4 C= -30The answers for AX2 + BX + C = 0 should be 3 and -5.x1=3x2=-5 bool solver(float...
Please Sign up or sign in to vote.
15 Nov 2010Michael Waters
I am reposting this because I accidentally deleted my first post.The solution is presented as a pair of complex roots, unless a == 0, in which case the equation is linear and returns a single complex(real) root.If a == 0 and b == 0, the function fails.Given a complex type, replace...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions