-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadraticEquation.java
42 lines (39 loc) · 1.09 KB
/
QuadraticEquation.java
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
33
34
35
36
37
38
39
40
41
42
//Program to solve quadratic equations (use if, else if and else).
import java.util.Scanner;
import java.lang.Math;
public class QuadraticEquation
{
public static void main(String args[])
{
Scanner reader = new Scanner(System.in);
System.out.print("\nEnter the coeffiect for quadratic term: ");
double a = reader.nextDouble();
System.out.print("\nEnter the coeffiect for linear term: ");
double b = reader.nextDouble();
System.out.print("\nEnter the constant term: ");
double c = reader.nextDouble();
reader.close();
double d = b*b - 4*a*c;
if (d > 0)
{
double root1 = (-b + Math.pow(d, 0.5))/(2*a);
double root2 = (-b - Math.pow(d, 0.5))/(2*a);
System.out.print("Both roots are real which are " + root1 + " and " + root2);
}
else if (d == 0)
{
System.out.print("Both roots are real and equal which is " + -b/(2*a));
}
else
{
System.out.print("Both roots are imaginary.");
}
}
}
//Output:
//Enter the coeffiect for quadratic term: 1
//
//Enter the coeffiect for linear term: 2
//
//Enter the constant term: 1
//Both roots are real and equal which is -1.0