Advertisement
CR7CR7

Quadratic equation

Jan 15th, 2023
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.*;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         double a = Double.parseDouble(scanner.nextLine());
  7.         double b = Double.parseDouble(scanner.nextLine());;
  8.         double c = Double.parseDouble(scanner.nextLine());;
  9.         double x1, x2;
  10.         double det = (b * b) - (4.0 * a * c);
  11.         double sqrt = Math.sqrt(det);
  12.         DecimalFormat pattern = new DecimalFormat("0.0");
  13.         if(det > 0){
  14.             x1 = (-b - sqrt) / (2*a);
  15.             x2 = -(b - sqrt) / (2*a);
  16.             System.out.print("x1=" +pattern.format((x1))+";"+" ");
  17.             System.out.println("x2=" + pattern.format((x2)));
  18.         }
  19.         else if(det == 0){
  20.             x1=x2=-b / (2*a);
  21.             System.out.print("x1=" + pattern.format((x1))+";"+" ");
  22.             System.out.println("x2=" + pattern.format((x2)));
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement