- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java Program to Find all Roots of a Quadratic Equation

In this article, we will understand how to calculate the roots of a quadratic equation in Java. A quadratic equation is an algebraic expression of the second degree or in other words, it has two results i.e. real number and an imaginary number.

Below is a demonstration of the same −

Given a quadratic equation of the form ax2 + bx + c −

There are three cases: b2 < 4*a*c - The roots are not real i.e. they are complex b2 = 4*a*c - The roots are real and both roots are the same. b2 > 4*a*c - The roots are real and both roots are different

**Input**

Suppose our input is −

a = 1, b = 2, c = 3

**Output**

The desired output would be −

The roots of the quadratic equation are root_1 = -1.00+1.41i root_2 = -1.00-1.41i

## Algorithm

Step1- Start Step 2- Declare 6 double values: a, b, c, root_1, root_2, quadratic_equation Step 3- Prompt the user to enter a,b,c double values/ define the double values Step 4- Read the values Step 5- In a for loop, check if the value of quadratic_equation variable is greater than 0, and if true, use quadric formula to find the value, and assign it to a variable. Step 6- Display the result Step 7- Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner; public class QuadraticEquation { public static void main(String[] args) { double a, b, c, root_1, root_2, quadratic_equation; double real_number, imaginary_number; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A scanner object has been defined "); System.out.print("Enter the value of a : "); a = my_scanner.nextDouble(); System.out.print("Enter the value of b : "); b = my_scanner.nextDouble(); System.out.print("Enter the value of c : "); c = my_scanner.nextDouble(); quadratic_equation = b*b - 4*a*c ; if (quadratic_equation > 0) { root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a); root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a); System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2); } else if (quadratic_equation == 0) { root_1 = root_2 = -b / (2 * a); System.out.format("root_1 = root_2 = %.2f;", root_1); } else { real_number = -b / (2 * a); imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a); System.out.println("The roots of the quadratic equation are"); System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number); System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number); } } }

## Output

Required packages have been imported A scanner object has been defined Enter the value of a : 1 Enter the value of b : 2 Enter the value of c : 3 The roots of the quadratic equation are root_1 = -1.00+1.41i root_2 = -1.00-1.41i

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class QuadraticEquation { public static void main(String[] args) { double a, b, c, root_1, root_2, quadratic_equation; double real_number, imaginary_number; a = 1; b = 2; c = 3; System.out.println("The three numbers are defined as " +a +", " +b +" and " +c); quadratic_equation = b*b - 4*a*c ; if (quadratic_equation > 0) { root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a); root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a); System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2); } else if (quadratic_equation == 0) { root_1 = root_2 = -b / (2 * a); System.out.format("root_1 = root_2 = %.2f;", root_1); } else { real_number = -b / (2 * a); imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a); System.out.println("The roots of the quadratic equation are"); System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number); System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number); } } }

## Output

The three numbers are defined as 1.0, 2.0 and 3.0 The roots of the quadratic equation are root_1 = -1.00+1.41i root_2 = -1.00-1.41i

- Related Articles
- C++ Program to Find All Roots of a Quadratic Equation
- Kotlin Program to Find all Roots of a Quadratic Equation
- Haskell program to find all roots of a quadratic equation
- Java program to find the roots of a quadratic equation
- How to Find all Roots of a Quadratic Equation in Golang?
- C program to find the Roots of Quadratic equation
- How to write a C program to find the roots of a quadratic equation?
- Finding roots of a quadratic equation – JavaScript
- Write all the values of k for which the quadratic equation $x^2+kx+16=0$ has equal roots. Find the roots of the equation so obtained.
- Find the quadratic roots in the equation$4x^{2}-3x+7$
- Program to find number of solutions in Quadratic Equation in C++
- Find $p$, if quadratic equation $py( y-2)+6=0$ has equal roots.
- Find the roots of the quadratic equation $sqrt{2}x^{2}+7x+5sqrt{2}=0$.
- Find the roots of the following quadratic equation:$x^{2} -3sqrt {5} x+10=0$
- A quadratic equation with integral coefficient has integral roots. Justify your answer.

Advertisements