Showing posts with label Java Interview QA. Show all posts
Showing posts with label Java Interview QA. Show all posts
Java Program to Find All Roots of a Quadratic Equation

Java Program to Find All Roots of a Quadratic Equation

 

Java Program to Find All Roots of a Quadratic Equation Using Formula

Quadratic Equation

 

The form of the quadratic equation is ax² + bx + c = 0 where a, b and c are real and a !=0, x is an unknown variable. The nature of roots is determined by the discriminant. The formula to find the roots of the quadratic is

 

quadratic equation

Java Program to Find all Roots of a Quadratic equation

 In this program, we have taken three values in a, b, and c and calculate 

discriminant = (b * b) - (4 * a * c). After check 

1. if discriminant > 0 then 

 The roots are unequal and irrational

2. if discriminant = 0 then 

The roots are equal and rational

3 otherwise

The roots are imaginary

import java.util.*;

class QuadraticEquation

{

    public static void main(String args[])

    {

        double a, b, c;

        double root, root1, root2;

        double disc; // discriminant

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter value of a, b, c");

        a= sc.nextInt();

        b= sc.nextInt();

        c= sc.nextInt();

       // Calculate discriminant

        disc = (b * b) - (4 * a * c);

        if ( disc > 0 )

        {

            root1 = (- b + Math.sqrt(disc)) / (2 * a);

            root2 = (- b - Math.sqrt(disc)) / (2 * a);

            System.out.println("The roots are unequal and irrational " + root1 + " and " + root2);

           

        }

        else if ( disc == 0 )

        {

            root = - b / (2 * a);

            System.out.println("The roots are equal and rational " + root + " and " + root );

        }

        else if ( disc < 0 )

        {

            System.out.println("The roots are imaginary");

        }

    } // function is over

} // class is over

Output

Enter value of a, b, c

20

30

20

The roots are imaginary

Enter value of a, b, c

2

7

3

The roots are unequal and irrational -0.5 and -3.0

 

Java program to find the Roots of Quadratic equation with method

 import java.util.*;

class QuadraticEquationWithMethod

{

    double a, b, c;

    double disc; // discriminant

    double root, root1, root2;

    // quadratic equation method

    void quadraticEquation(double x, double y, double z)

    {

        a = x;

        b = y;

        c = z;

        disc = (b * b) - (4 * a * c);

        if ( disc > 0 )

        {

            root1 = (- b + Math.sqrt(disc)) / (2 * a);

            root2 = (- b - Math.sqrt(disc)) / (2 * a);

            System.out.println("The roots are unequal and irrational " + root1 + " and " + root2);

           

        }

        else if ( disc == 0 )

        {

            root = - b / (2 * a);

            root1 = root;

            System.out.println("The roots are equal and rational " + root + " and " + root1 );

        }

        else if ( disc < 0 )

        {

            System.out.println("The roots are imaginary");

        }

    }

    // main method   

    public static void main(String args[])

    {

        double a, b, c;    

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter value of a, b, c");

        a= sc.nextInt();

        b= sc.nextInt();

        c= sc.nextInt();

        QuadraticEquationWithMethod ob=new QuadraticEquationWithMethod();

        // calling method

        ob.quadraticEquation(a, b, c);

       

    } // main method is over

} // class is over

 

Output:
Enter value of a, b, c

1

1

1

The roots are imaginary

Enter value of a, b, c

1

6

11

The roots are imaginary

Enter value of a, b, c

1

7

12

The roots are unequal and irrational -3.0 and -4.0

More Java programs 


Comments in java code with Example - java comments

Comments in java code with Example - java comments

 

Comments in java code

Comments in java code

The comment in the computer programming language is a piece of code for user understanding and they are added to help humans to understand and easy to read. The comment is generally not read by the compiler and interpreter. And comments are a very essential part of computer programming language.

How many types of comments in java?

 There are three types of comments in java

  1. Single line comments
  2. Multi-line comments
  3. Java doc comments

Single line comments in java (//): This comment is used in single line code

Syntax:

// This symbol is a single-line comment  

 

Example

// class started (Single line comment)

public  class Summation

{       

          public static void main(String args[])

{

          int a, b, c; // Local variable

          a = 10;

          b = 30;

          c = a + b;

          System.out.println(c);

}

} // class end

Multi line comments in java (/*   */): 

This symbol is used for multi-line comment

Syntax:

/*

  This symbol is an example

  Of multi-line comment  

*/


Example

/* class started Multi line comment)

    Comment contains more then

     One-line

*/

public  class Summation

{       

          public static void main(String args[])

{

         

/* a, b and c

    Are local variable

*/

int a, b, c;  

          a = 50;

          b = 70;

          c = a + b;

          System.out.println(c);

}

} // class end

Java document comments

This comment more or less the same as the multi-line comment with one difference, it start with /** (with two-star) and it is used to generate Javadoc comment.

 

/**

  This program takes two number in a, b

   And add the number to variable c

   And print the c

*/

class MyClass

{

         public static void main(String args[])

{         

int a, b, c;  

          a = 80;

          b = 90;

          c = a + b;

          System.out.println(“Summation=” + c);

}

}

Java program