*Java program to find roots of quadratic equation*

*Java program to find roots of quadratic equation*

Write a Java program to find roots of quadratic equation in all cases. This Java Program To Compute Roots of Quadratic Equation makes use of **If – Else** Block.

**What is a Quadratic Equation?**

It is a term used in Elementary Algebra. The Standard Form of a Quadratic Equation is **ax ^{2} + bx + c = 0, **where

**a**,

**b**,

**c**are constant values which cannot be changed and

**x**is a variable entity.

**Quadratic Equation Formula**

r = −b ± √(b^{2}− 4ac) / 2a

**Conditions For Discriminants**

- If
b, One Real Solution is possible^{2}− 4ac = 0- If
bis^{2}− 4acPositive, Two Real Solutions are possible- If
bis^{2}− 4acNegative, Two Complex Solutions are possible

- Enter the Quadratic Equation as an input. Now we use the given formulas to calculate the roots of the equation.The
**java.lang.***package consists of**Math.sqrt()**method which helps to calculate the Square Root of the Discriminant.

- Here is the source code of the Java Program to Find the Roots of Quadratic Equation. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

**SOURCE CODE : :**

**SOURCE CODE : :**

import java.util.Scanner; public class quadratic_equation { public static void main(String args[]) { double a, b, c; double root1, root2, D, sqrroot; System.out.println("\nEnter The Values"); Scanner sc = new Scanner(System.in); System.out.print("\nA: "); a = sc.nextDouble(); System.out.print("\nB: "); b = sc.nextDouble(); System.out.print("\nC: "); c = sc.nextDouble(); System.out.println("\nGiven quadratic equation : "+a+"x^2 + "+b+"x + "+c); if((a==0)&&(b==0)&&(c==0)) { System.out.println("\nEnter atleast two non-zero co-efficients"); System.exit(0); } else if(a==0 && b!=0) { root1= -c/b; System.out.println("\nThe roots are:"+root1); System.exit(0); } else if(a==0&&b==0&&c!=0) { System.out.println("\nThe equation has no solution"); System.exit(0); } else { D = b*b - 4*a*c; sqrroot = Math.sqrt(D); if(D<0) { System.out.println("\nRoots Are Imaginary\n"); } else if(D == 0) { System.out.println("\nRoots are real and equal"); root1 = (-b + sqrroot) / (2*a); System.out.println("\nRoots are : "+root1); } else { System.out.println("\nRoots are real and unequal"); root1 = (-b + sqrroot) / (2*a); root2 = (-b - sqrroot) / (2*a); System.out.println("\nRoot 1 = " + root1 + "\n"); System.out.println("Root 2 = " + root2 + "\n"); } } } }

**OUTPUT : :**

**OUTPUT : :**

Enter The Values A: 2 B: 8 C: 4 Given quadratic equation : 2.0x^2 + 8.0x + 4.0 Roots are real and unequal Root 1 = -0.5857864376269049 Root 2 = -3.414213562373095

__Description-__

__Description-__

- This very simple program inputs the quadratic equation by inputting the coefficients of x2 ,x and constant term.

- After that the value of discriminant is calculated by its formula Discriminant=b^2-4ac.

- As we know that the roots of a Quadratic equation depends largely on the value of discriminant ,three cases occur :

If discriminant is

than twogreater than zeroare obtained , which are calculated and given by Dharacharya formula.distinct and real rootsIf discriminant is

then it means that the equation is a Perfect Square and twozeroare obtained.equal rootsIf discriminant is

then no real roots are obtained and thus it hasless than zeroand since no datatype of JAVA can hold imaginary value, no solution can be represented here.no real solution

Java program to find roots of quadratic equation but in

modular design.