Program to implement a Quadratic equation in Java

In this program, you will learn to find all roots of a quadratic equation in Java.

Also, you can use the Java compiler to compile a program.

Program to implement a Quadratic equation in Java

Program to implement all Roots of a Quadratic equation in java

The standard form of equation is:

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0

The term b2-4ac is known as the determinant of an equation. The determinant tells the nature of the roots.

  • If the determinant is greater than 0, the roots are real and different.
  • And determinant is equal to 0, the roots are real and equal.
  • If the determinant is less than 0, the roots are complex and different.
SOURCE CODE.
import java.util.*;
class quadratic3
{
	public static void main(String args[])
	{
		int e,a,b,c;
		double l,x,y;
		Scanner s= new Scanner(System.in);
		System.out.println("Enter value a,b,c:");
		a=s.nextInt();
		b=s.nextInt();
		c=s.nextInt();
		
		e=b*b-4*a*c;
		if(e<0)
		{
			System.out.println("NOT REAL");
		}
			else if(e == 0) 
		{
                 x = -b / (2.0 * a);
                System.out.println("The root is " + x);
		}
		else 
		{
			l=Math.sqrt(e);
			x= (-b+l)/(2*a);
			y= (-b-l)/(2*a);
			System.out.println("equation= "+x+" & "+y);
		}
	}// END OF MAIN
}//END OF CLASS	

OUTPUT.

C:\A>javac quadratic3.java
C:\A>java quadratic3
Enter value a,b,c:
1
4
4
The root is -2.0

OR

C:\A>javac quadratic3.java
C:\A>java quadratic3
Enter value a,b,c:
1
5
2
equation= -0.4384471871911697 & -4.561552812808831

Also view: – Command-Line program find the Largest number

About Ashishkumar Vishwakarma

I am Ashish- a Developer live in Mumbai.

View all posts by Ashishkumar Vishwakarma →

3 Comments on “Program to implement a Quadratic equation in Java”

  1. I think this is among the most vital info for me. And i’m glad reading your article. But should remark on few general things, The site style is great, the articles is really nice : D. Good job, cheers

  2. Hey very nice web site!! Man .. Beautiful .. Superb .. I will bookmark your blog and take the feeds additionally…I am happy to search out so many helpful information here within the publish, we want work out extra strategies on this regard, thanks for sharing.

Leave a Reply