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

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

  1. ….hello again….

    hats off to u 4 publishing sumthin wich i really cant understand…i really dnt knw wat kind of thing u r
    using….in d program…!!!!

    plz make changes so dat student lik me can understand

    …………plz………

    Reply
  2. seenu.pasham

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

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

    Reply
  3. Divyashree

    I want a program to enter our own equation to find roots…..

    Reply

Leave a Reply