Java program to 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 solutions.

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
27
28
29
30
31
32
import java.io.*;
class Quadratic
{
	public static void main(String args[])throws IOException
	{
		double x1,x2,disc,a,b,c;
		InputStreamReader obj=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(obj);
		System.out.println("enter a,b,c values");
		a=Double.parseDouble(br.readLine());
		b=Double.parseDouble(br.readLine());
		c=Double.parseDouble(br.readLine());
		disc=(b*b)-(4*a*c);
		if(disc==0)
		{
			System.out.println("roots are real and equal ");
			x1=x2=-b/(2*a);
			System.out.println("roots are "+x1+","+x2);
		}
		else if(disc>0)
		{
			System.out.println("roots are real and unequal");
			x1=(-b+Math.sqrt(disc))/(2*a);
			x2=(-b+Math.sqrt(disc))/(2*a);
			System.out.println("roots are "+x1+","+x2);
		}
		else
		{
			System.out.println("roots are imaginary");
		}
	}
}
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.

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

  1. Scanner class is used to get the input from the user.
    ex:
    int x;
    Scanner sc = new Scanner();
    x=sc.nextInt();
    system.out.print(x);

  2. import java.util.Scanner;
    public class EkuacioniKuadratik
    {
    public static void main(String[] arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.print(“Vlera e a: “);
    double a=sc.nextDouble();
    System.out.print(“Vlera e b: “);
    double b=sc.nextDouble();
    System.out.print(“Vlera e c: “);
    double c=sc.nextDouble();

    if(a==0&&b!=0)
    {
    System.out.println(“Ekuacioni eshte linear dhe x= “+(-c/b));
    System.exit(0);
    }
    if(a==0&&b==0&&c!=0)
    {
    System.out.println(“Ekuacioni nuk ka zgjidhje”);
    System.exit(0);
    }
    if(a==0&&b==0&&c==0)
    {
    System.out.print(“Sistemi eshte i pacaktuar”);
    System.exit(0);
    }
    double d=Math.pow(b,2)-4*a*c;

    if(d<0)
    {
    d=Math.abs(d);
    double x1=(-b+Math.sqrt(d))/2*a;
    double x2=(-b-Math.sqrt(d))/2*a;
    System.out.println("x1="+x1+"i , x2="+x2+"i");
    }
    else{
    double x1=(-b+Math.sqrt(d))/2*a;
    double x2=(-b-Math.sqrt(d))/2*a;
    System.out.println("x1="+x1+" ,x2="+x2);
    }
    }

    }

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