Write a Program in java that prints all real solutions to the quadratic equations ax2+bx+c=0. Read a,b,c and use the quadratic formula. If the discriminate b2-4ac is negative, display a message stating there are no real solutions.
Write a Program in java that prints all real solutions to the quadratic equations ax2+bx+c=0.
Read a,b,c and use the quadratic formula.
If the discriminate b2-4ac is negative, display a message stating there are no real solutions.
import java.util.Scanner;
class quads{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Eneter the value of a : ");
double a = sc.nextDouble();
System.out.println("Enter the value of b : ");
double b = sc.nextDouble();
System.out.println("Enter the value of c : ");
double c = sc.nextDouble();
double d = b * b - 4 * a * c;
if (d > 0.0) {
double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
}
else {
System.out.println("Roots are not real.");
}
sc.close();
}
}
OUTPUT:
Enter the value of a :
1
Enter the value of b :
5
Enter the value of c :
2
The roots are -0.4384471871911697 and -4.561552812808831
Comments
Post a Comment
Please do not enter any spam link in the comment box.