Write a Java program to find roots of quadratic equation in all cases

By | 14.03.2017

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 ax2 + 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 ± √(b2 − 4ac) / 2a

Conditions For Discriminants

  1. If b2 − 4ac = 0, One Real Solution is possible
  2. If b2 − 4ac is Positive, Two Real Solutions are possible
  3. If b2 − 4ac is Negative, 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 : :


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 : :


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-


  • 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 greater than zero than two distinct and real roots are obtained , which are calculated and given by Dharacharya formula.

If discriminant is zero then it means that the equation is a Perfect Square and two equal roots are obtained.

If discriminant is less than zero then no real roots are obtained and thus it has no real solution and since no datatype of JAVA can hold imaginary value, no solution can be represented here.

0 0 votes
Article Rating
Category: Java Programming Number Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Lola

Java program to find roots of quadratic equation but in modular design.