Question

Java Programming Question. I am writing a code to calculate the roots of the quadratic equation...

Java Programming Question.

I am writing a code to calculate the roots of the quadratic equation based on the values of the coefficients A, B, and C. I am supposed to enter 5 values for each coefficient, and the output should be five different answers (see example) using a for loop and if else statements. However, when I run my code, I am only able to enter one value for each coefficient and the output is one answer repeated five times. How do I fix this? My code is below.

For example enter the following for A, B, and C:

A B C

5 2 2

0 0 4

0 3 6

1 2 1

2 5 2

import java.util.Scanner;
public class Quatratic
{
public static void main (String [] args)
{
int i, CoeffA, CoeffB, CoeffC;
double Disc, X1, X2;
  
Scanner sc = new Scanner (System.in);
  
//Read in coefficients A, B, and C.
System.out.println ("Enter the five coefficients for each A, B, and C");
  
CoeffA = sc.nextInt();
CoeffB = sc.nextInt();
CoeffC = sc.nextInt();
  
//Calculate discriminant.
Disc = (CoeffB * CoeffB) - (4 * CoeffA * CoeffC);
  
//For loop.
for (i = 0; i < 5; i++)
{
// If disciminant and coefficient A are greater than zero, calculate the two roots.
if (Disc > 0 && CoeffA > 0)
{
X1 = CoeffB + Math.sqrt (Disc) / (2 * CoeffA);
X2 = CoeffB - Math.sqrt (Disc) / (2 * CoeffA);
System.out.println ("The equation has roots " + X1 + " and " + X2);
}
//If coefficient A and B are equal to zero, there are no roots.
else if (CoeffA == 0 && CoeffB == 0)
{
System.out.println ("The equation has no roots");
}
//If coefficient A is equal to zero, calculate the single root.
else if (CoeffA == 0)
{
X1 = - CoeffC / CoeffB;
System.out.println ("The equation has a single root equal to " + X1);
}
//If discriminant is less than zero, there are no real roots.
else if (Disc < 0)
{
System.out.println ("The equation has no real roots (imaginary)");
}
//If discriminant is equal to zero, calculate the two roots.
else if (Disc == 0)
{
X1 = - CoeffB / (2 * CoeffA);
System.out.println ("The equation has two roots equal to " + X1);
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have to take the input with in the for loop

Follow \;the \;code \;below.\;

\color{blue}\underline{Quatratic.java:}

import java.util.Scanner;
public class Quatratic
{
    public static void main (String [] args)
    {
        int i, CoeffA, CoeffB, CoeffC;
        double Disc, X1, X2;

        Scanner sc = new Scanner (System.in);

        //Read in coefficients A, B, and C.
        System.out.println ("Enter the five coefficients for each A, B, and C");

        //For loop.
        for (i = 0; i < 5; i++)
        {
            CoeffA = sc.nextInt();
            CoeffB = sc.nextInt();
            CoeffC = sc.nextInt();

            //Calculate discriminant.
            Disc = (CoeffB * CoeffB) - (4 * CoeffA * CoeffC);
            
            // If disciminant and coefficient A are greater than zero, calculate the two roots.
            if (Disc > 0 && CoeffA > 0)
            {
                X1 = CoeffB + Math.sqrt (Disc) / (2 * CoeffA);
                X2 = CoeffB - Math.sqrt (Disc) / (2 * CoeffA);
                System.out.println ("The equation has roots " + X1 + " and " + X2);
            }
            //If coefficient A and B are equal to zero, there are no roots.
            else if (CoeffA == 0 && CoeffB == 0)
            {
                System.out.println ("The equation has no roots");
            }
            //If coefficient A is equal to zero, calculate the single root.
            else if (CoeffA == 0)
            {
                X1 = - CoeffC / CoeffB;
                System.out.println ("The equation has a single root equal to " + X1);
            }
            //If discriminant is less than zero, there are no real roots.
            else if (Disc < 0)
            {
                System.out.println ("The equation has no real roots (imaginary)");
            }
            //If discriminant is equal to zero, calculate the two roots.
            else if (Disc == 0)
            {
                X1 = - CoeffB / (2 * CoeffA);
                System.out.println ("The equation has two roots equal to " + X1);
            }
        }
    }
}

\color{red}\underline{Output:}

Enter the five coefficients for each A, B, and C 5 2 2 The equation has no real roots (imaginary) The equation has no roots T?

Add a comment
Know the answer?
Add Answer to:
Java Programming Question. I am writing a code to calculate the roots of the quadratic equation...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is in python language Algebra: solve quadratic equations) The two roots of a quadratic equation,...

    This is in python language Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, 0, can be obtained using the following fomula: b 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots. Write a program that prompts the user to enter values for a, b, and cand...

  • (Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, , can be...

    (Algebra: solve quadratic equations) The two roots of a quadratic equation, for example, , can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r1 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no real roots....

  • In Python. The two roots of a quadratic equation ax^2 + bx + c = 0...

    In Python. The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r2 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...

  • Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c...

    Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c 0. The class contains: • The data fields a, b, and c that represent three coefficients. . An initializer for the arguments for a, b, and c. • Three getter methods for a, b, and c. • A method named get Discriminant() that returns the discriminant, which is b- 4ac The methods named getRoot 1() and getRoot 2() for returning the two roots of...

  • C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠...

    C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • Rule book: 1.)Must be Python - 2.) no imported module (ie cmath) - no can do...

    Rule book: 1.)Must be Python - 2.) no imported module (ie cmath) - no can do on the imports... pure code.. 3.) numbers are float - not integer. Here's the prompt: (Algebra: solve quadratic equations) The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a) b^2 - 4ac is called...

  • Using C++ For this program you are going to create a class that helps to solve...

    Using C++ For this program you are going to create a class that helps to solve the quadratic equation. The equation has the following form: ax2 + bx + c = 0 The roots of the equation are: x1 = -b + sqrt(b2 - 4 * a * c) and x2 = -b - sqrt(b2 - 4 * a * c) The phrase in the parenthesis is called the discriminant. If the value of the discriminant is positive, the equation...

  • Java Branches code not working please HELP!! I am trying to build this class that will...

    Java Branches code not working please HELP!! I am trying to build this class that will ask for an ingredient, and the number of cups of the ingredient. It should then branch into an if/else branch off to verify that the number entered is valid and that it is within range. Once that branch completes it should then continue on to ask for the calories per cup, and then calculate total calories. Once I get it to enter the branch...

  • help KKKL1164 4. The C program presented below has some syntax and logic errors. Analyse it...

    help KKKL1164 4. The C program presented below has some syntax and logic errors. Analyse it and highlight the errors, explaining how the errors can be corrected. This program attempts to solve a quadratic equation. #include <stdio.h> #include <math.h> int Main() const int a, b, Ci double doro, disc printf ("Lets find the root (s) of a printf ("ax 2 bx c. \n"); printf ("Please enter the coefficient a: scanf ("d", &c); printf ("Please enter the coefficient b: "); scanf...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT