Results 1 to 4 of 4

Thread: Quadratic Equation Program

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Resolved 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

    Code:
    // 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");
    }

  2. #2

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Quadratic Equation Program

    actually want to use cout and cin instead of printf..

    how would i implement this into my code!?

    Thank You!

    that way i can use flag values for justification and fixed/scientific

    for couts

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Quadratic Equation Program

    this is what I have now:

    Code:
    // Quadratic Equation.cpp : main project file.
    #include<stdio.h>
    #include<math.h>
    #include <iostream>	
    #include <iomanip>							 
    using namespace std;
    
    int main()
    {
          float A, B, C;
          double disc, deno, x1, x2;
          cout << "ENTER THE VALUES OF A,B,C...";
    	  cin >> A >> B >> C;
          scanf_s("&#37;d,%d,%d", &A, &B, &C);
          disc = (B * B) - (4 * A * C);
          deno = 2 * A;
          if(disc > 0)
          {
                 cout<<"\n\t THE ROOTS ARE REAL ROOTS"<<endl;;
                 x1 = (-B/deno) + (sqrt(disc) / deno);
                 x2 = (-B/deno) - (sqrt(disc) / deno);
                 cout <<"\n\n\t THE ROOTS ARE...: %f and %f\n"<< x1 << x2 <<endl;;
          }
          else if(disc == 0)
          {
                 cout<<"\n\t THE ROOTS ARE REPEATED ROOTS"<<endl;;
                 x1 = -B/deno;
                 cout<<"\n\n\t THE ROOT IS...: %f\n", x1<<endl;;
          }
          else
                 cout<<"\n\t THE ROOTS ARE IMAGINARY ROOTS\n"<<endl;;
    }

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2009
    Posts
    35

    Re: Quadratic Equation Program

    so how would i implement the outputs and conditions like i said above into my code below???


    changed my code:

    Code:
    // 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;
    
    }
    :check:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width