-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoots_of_quadratic_eq.java
More file actions
43 lines (40 loc) · 1.27 KB
/
Roots_of_quadratic_eq.java
File metadata and controls
43 lines (40 loc) · 1.27 KB
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
43
package Programs;
import java.util.Scanner;
import static java.lang.Math.round;
import static java.lang.Math.sqrt;
public class Roots_of_quadratic_eq {
public static void main(String[] args) {
// Write your code here
int a,b,c;
Scanner s=new Scanner(System.in);
System.out.println("For the equation in the form of : a(x^2) + bx + c = 0");
System.out.print("Enter the value of a : ");
a=s.nextInt();
System.out.print("Enter the value of b : ");
b=s.nextInt();
System.out.print("Enter the value of c : ");
c=s.nextInt();
calcroot(a,b,c);
}
static void calcroot(int a,int b,int c)
{
float d;
double x,y;
d=(b*b)-4*a*c;
x=((-b+sqrt(d))/(2*a));
y=((-b-sqrt(d))/(2*a));
if(d>0)
{
System.out.println("Roots of equation "+a+"x^2 + "+b+"x + "+c+" are real and unequal and their values are : ");
System.out.print(round(x)+" "+round(y));
}
else if(d<0) {
System.out.println("No Roots for this Equation");
System.out.print(-1);}
else
{
System.out.println("Roots of equation "+a+"x^2 + "+b+"x + "+c+" are real and equal and their value is : ");
System.out.print(round(x)+" "+round(y));
}
}
}