Today, I tried to translate the very basic "solve quadratic equations" program I made a while back in Python to C++.(adsbygoogle = window.adsbygoogle || []).push({});

#Solving quadratic equations

import math

print "Please enter information in accordance with ax^2 + bx + c = 0"

a = input("What is a?")

b = input("What is b?")

c = input("what is c?")

b = float(b) / float(a)

c = float(c) / float(a)

before_the_square = 0.5 * b * -1

the_square = (0.5 * b)**2 - c

if the_square < 0:

print "Imaginary" #fail-safe for imaginary numbers.

else:

value_of_square = math.sqrt(the_square)

x1 = before_the_square + value_of_square

x2 = before_the_square - value_of_square

print "x(1) is:"

print x1

print "x(2) is:"

print x2

I tried something like this in C++:

// Solving quadratic equations

#include <iostream>

#include <string>

#include <math.h>

using namespace std;

main ()

{

float a, b, c, before_sqrt, inside_sqrt, after_sqrt, x1, x2;

cout << "Solving quadratic equation" << endl << endl;

cout << "ax^2 + bx + c = 0";

cin >> a, b, c;

b = b / a;

c = c / a;

before_sqrt = (b/2)*-1;

inside_sqrt = pow(0.5b,2) - c;

if (inside_sqrt < 0)

{

cout << "Error";

}

else

after_sqrt = sqrt (inside_sqrt);

x1 = before_sqrt + after_sqrt;

x2 = before_sqrt - after_sqrt;

cout "x1 =" << endl << endl << x1;

cout "x2 =" << endl << endl << x2;

system("pause");

return 0;

}

The indent is wrong due to copy/pasting and I have not removed the scaffolding.

I'm pretty sure I made some mistakes with the declaration in the beginning as well as the difficulty to use exponential with variables (red), altohugh I'm not sure how to solve it. Any help is greatly appreciated.

Thank you for your time

**Physics Forums - The Fusion of Science and Community**

# C++ and solving quadratic Equations

Know someone interested in this topic? Share a link to this question via email,
Google+,
Twitter, or
Facebook

- Similar discussions for: C++ and solving quadratic Equations

Loading...

**Physics Forums - The Fusion of Science and Community**