Define a function void Roots( double a, double b, double c ) to find the results of x1, x2 from the
following relation. Print the values of a, b, c and values of x1, x2 as output.
xl =
-b+ √b² - 4ac
2a
and x2 =
-b - √b² - 4ac
2a
Answers
import java.util.Scanner;
import java.lang.Math;
public class main{
public static void main(String[] str)
{
System.out.println("Enter 1st number: ");
Scanner input = new Scanner(System.in);
double num = input.nextDouble();
System.out.println("Enter 2nd number: ");
Scanner input2 = new Scanner(System.in);
double num2 = input2.nextDouble();
System.out.println("Enter 3rd number: ");
Scanner input3 = new Scanner(System.in);
double num3 = input3.nextDouble();
System.out.println(root(num,num2,num3));
System.out.println(root2(num,num2,num3));
}
public static double root (double a, double b ,double c)
{
double x1;
x1 = -b + Math.sqrt(b*b) - 4*a*c + 2*a; // you didnot wrote any operator after -4ac, so I put + operator myself you can replace it with any valid operator
return x1;
}
public static double root2 (double a, double b ,double c)
{
double x2;
x2 = -b - Math.sqrt(b*b) - 4*a*c + 2*a; // you didnot wrote any operator after -4ac, so I put + operator myself you can replace it with any valid operator
return x2;
}
}