- 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 the roots of a quadratic equation
Roots of a quadratic equation are determined by the following formula:
$$x = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$
To calculate the roots −
- Calculate the determinant value (b*b)-(4*a*c).
- If determinant is greater than 0 roots are [-b +squareroot(determinant)]/2*a and [-b -squareroot(determinant)]/2*a.
- If determinant is equal to 0 root value is (-b+Math.sqrt(d))/(2*a)
Example
import java.util.Scanner; public class RootsOfQuadraticEquation { public static void main(String args[]){ double secondRoot = 0, firstRoot = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter the value of a ::"); double a = sc.nextDouble(); System.out.println("Enter the value of b ::"); double b = sc.nextDouble(); System.out.println("Enter the value of c ::"); double c = sc.nextDouble(); double determinant = (b*b)-(4*a*c); double sqrt = Math.sqrt(determinant); if(determinant>0){ firstRoot = (-b + sqrt)/(2*a); secondRoot = (-b - sqrt)/(2*a); System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot); }else if(determinant == 0){ System.out.println("Root is :: "+(-b + sqrt)/(2*a)); } } }
Output
Enter the value of a :: 15 Enter the value of b :: 68 Enter the value of c :: 3 Roots are :: -0.044555558333472335 and -4.488777774999861
- Related Articles
- Java Program to Find all Roots of a Quadratic Equation
- C program to find the Roots of Quadratic equation
- C++ Program to Find All Roots of a Quadratic Equation
- Haskell program to find all roots of a quadratic equation
- Kotlin Program to Find all Roots of a Quadratic Equation
- How to write a C program to find the roots of a quadratic equation?
- How to Find all Roots of a Quadratic Equation in Golang?
- Finding roots of a quadratic equation – JavaScript
- Find the quadratic roots in the equation$4x^{2}-3x+7$
- 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 roots of the following quadratic equation:$x^{2} -3sqrt {5} x+10=0$
- Find the roots of the quadratic equation $sqrt{2}x^{2}+7x+5sqrt{2}=0$.
- Program to find number of solutions in Quadratic Equation in C++
- If $1$ is a root of the quadratic equation $3x^2 + ax - 2 = 0$ and the quadratic equation $a(x^2 + 6x) - b = 0$ has equal roots, find the value of b.
- If $2$ is a root of the quadratic equation $3x^2 + px - 8 = 0$ and the quadratic equation $4x^2 - 2px + k = 0$ has equal roots, find the value of k.

Advertisements