Take the 2-minute tour ×

I've been trying to look up on Google how to put in an equation in my program but wasn't able to find any. How do you include:


in the program?

Here's my code:

    int a, b, c;
    float x;

    printf("Enter three integers: ");



    printf("The value of x is %.1f", x);

    return 0;
share|improve this question
What programming language are you talking about here? –  Oli Charlesworth May 24 '11 at 23:52
Are you trying to evaluate the equation or are you simply trying to display the equation? This looks like one of the C languages like C++, is that correct? –  Thomas May 24 '11 at 23:53
this isn't correct or good practice. what if a is zero? b? c? There are special cases to account for. –  duffymo May 24 '11 at 23:55
Also, are we to assume the second portion of the equation is the square root of b then multiplied by 2 or is it the square root of the result of b multiplied by 2? –  Thomas May 24 '11 at 23:58
@Thomas: I would infer that this is supposed to be the equation for solving a quadratic: en.wikipedia.org/wiki/Quadratic_formula#Quadratic_formula. –  Oli Charlesworth May 24 '11 at 23:59
show 7 more comments

3 Answers

Assuming we're talking about C (or C++) here, you will need to investigate the sqrt function, and maybe also the pow function as well (although that's unnecessary because b-squared can be computed as b*b).

Note that you will need to convert all of your input values to float or double before you start the calculation, otherwise you will not get the intended result.

share|improve this answer
and remember to change the scanf line, and the printf if we are being picky. –  ColWhi May 25 '11 at 0:00
@Sasquiha: Not necessarily! We could cast on the way into the formula, and the printf is already %f. –  Oli Charlesworth May 25 '11 at 0:06
And check for a negative discriminant (imaginary roots) before calling sqrt(). –  Jonathan Leffler May 25 '11 at 0:38
The guy is having basic problems without getting into casting. And I meant the printf above the scanf. –  ColWhi May 25 '11 at 0:38
add comment

You need a table to allow you to translate:

a+b -> a+b

a-b -> a-b

a/b -> a/b

ab -> a*b

√x -> sqrt(x)

x² -> x*x (If you want to square something more complicated it might be best to use a temporary variable for the value to be squared, breaking your equation up into pieces.)

Note that if you divide an int by an int in C you get an int. So better convert those ints to doubles before dividing.

share|improve this answer
add comment

If we are dealing with C++ it would be something like

#include <iostream.h>
#include <cmath>

int main ()

//Declare Variables
double x,x1,x2,a,b,c;
cout << "Input values of a, b, and c." ;
cin >>a >>b >>c;
    if ((b * b - 4 * a * c) > 0)
    cout << "x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)" &&
    cout << "x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)";

    if else ((b * b - 4 * a * c) = 0)
    cout << "x = ((-b + sqrt(b * b - 4 * a * c)) / (2 * a)"

    if else ((b * b - 4 * a * c) < 0)
    cout << "x1 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a) &&
    cout << "x2 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a);
return (0);

Now why do i have this wierd feeling I just did someone's first semester programming class' homework?

Granted its been years and I don't even know if that will compile but you should get the idea.

share|improve this answer
This most definitely won't compile! Also, it doesn't calculate or print a numerical result at any point... –  Oli Charlesworth May 25 '11 at 0:08
but the idea is there (using sqrt and doube and input/outputs) ... i have an uneasy feeling basically DOING people homework, but wanted to get him off on the right track... –  colinross May 25 '11 at 0:11
haha! not really, this is just a small fragment of the problem I needed to solve a bigger one. Good job and thanks for your help, i just needed to translate the equation to the editor :) –  redkimono May 25 '11 at 0:11
add comment

Your Answer


