Quadratic Equation Program

I have my program below (it runs and compiles as is) , but having difficulty implement these conditions and outputs:

conditions:
1. rejecting certain coefficients if the discriminat (B^(2) -4AC) <0.
2. also having no division by zero in my program (do i use setPrecision)?

outputs:
having at least 15 spaces or so for each of the root ouput and outputting the roots in the following format:
-right justified, four decimal places accuracy, fixed point format
-right justified, two decimal places accuracy, floating point (scientific) format
- left justified, four decimal places accuracy, fixed point format
- left justified, two decimal places accuracy, floating point (scientific) format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// Quadratic Equation.cpp : main project file.
#include<stdio.h>
#include<math.h>

int main()
{
      int A, B, C;
      double disc, deno, x1, x2;
      printf("\n\n\t ENTER THE VALUES OF A,B,C...");
      scanf("%d,%d,%d", &A, &B, &C);
      disc = (B * B) - (4 * A * C);
      deno = 2 * A;
      if(disc > 0)
      {
             printf("\n\t THE ROOTS ARE REAL ROOTS");
             x1 = (-B/deno) + (sqrt(disc) / deno);
             x2 = (-B/deno) - (sqrt(disc) / deno);
             printf("\n\n\t THE ROOTS ARE...: %f and %f\n", x1, x2);
      }
      else if(disc == 0)
      {
             printf("\n\t THE ROOTS ARE REPEATED ROOTS");
             x1 = -B/deno;
             printf("\n\n\t THE ROOT IS...: %f\n", x1);
      }
      else
             printf("\n\t THE ROOTS ARE IMAGINARY ROOTS\n");
}
also decided to use cout and cin instead of printf.
how would I also implement these cout and cin into my code along with the outputs and conditions?
this is what I have now: changed it so its cout and cin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Quadratic Equation.cpp : main project file.
#include <cmath>  // defines the sqrt() function
#include <iostream>
using namespace std;

int main()
{ // implements the quadratic formula
  float a, b, c;
 
  cout << "Enter the coefficients of a quadratic equation:" << endl;
  cout << "a: ";
  cin >> a;
  cout << "b: ";
  cin >> b;
  cout << "c: ";
  cin >> c;

  cout << "The roots of " << a << "*x*x + " << b
       << "*x + " << c << " = 0" << endl;
  
  float d = b*b - 4*a*c;  // discriminant
  float sqrtd = sqrt(d);
  float x1 = (-b + sqrtd)/(2*a);
  float x2 = (-b - sqrtd)/(2*a);
  
  cout << "are:" << endl;
  cout << "x1 = " << x1 << endl;
  cout << "x2 = " << x2 << endl;

}

Last edited on
any assistance with this?

how to implement these:
conditions:
1. rejecting certain coefficients if the discriminat (B^(2) -4AC) <0.
2. also having no division by zero in my program (do i use setPrecision)?

outputs:
having at least 15 spaces or so for each of the root ouput and outputting the roots in the following format:
-right justified, four decimal places accuracy, fixed point format
-right justified, two decimal places accuracy, floating point (scientific) format
- left justified, four decimal places accuracy, fixed point format
- left justified, two decimal places accuracy, floating point (scientific) format
1.) Check that condition and then do whatever based on it
2.) Same as #1, check if a == 0.

For the other stuff, read up on iostream manipulators like setPrecision().
Last edited on
Topic archived. No new replies allowed.