Java program that prints all real solutions to the quadratic equation ax2+bx+c=0

Write a Java program that prints all real solutions to the quadratic equation ax2+bx+c=0. Read in a,b,c and use the quadratic formula.If the discriminant b2-4ac is negative, display a message stating that there are no real roots.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;
 class solutions
{
	public static void main(String[] args)
	{
		int a,b,c;
		double x,y;
		Scanner s=new Scanner(System.in);
		System.out.println(“Enter the values of a,b, and c”);
		a=s.nextInt();
		b=s.nextInt();
		c=s.nextInt();
		int k=(b*b)-4*a*c;
		if(k<0)
		{
			System.out.println(“No real roots”);
		}
		else
		{
			double l=Math.sqrt(k);
			x=(-b-l)/2*a;
			y=(-b+l)/2*a;
			System.out.println(“Roots of given equation:+x+” “+y);
		}
	}
}

Output:

Enter the values of a,b,c
1
5
6
Roots of given equation:-3.0 -2.0

Enter the values of a, b, c
1
2
2
No real solutions

Enter the values of a, b, c
1
3
2
Roots of given equation: -2.0 -1.0

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

7 thoughts on “Java program that prints all real solutions to the quadratic equation ax2+bx+c=0

  1. Sir… I’ve a doubt.. Please clarify me…

    “why an array index starts from zero only? Is there any special reason for that?”

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in