Advertisement
Guest User

DecimalFormat

a guest
Feb 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.text.DecimalFormat;
  3. public class DecimalFormatt{
  4.     public static void main(String args[]){
  5.         int a,b,c;
  6.         double discriminant, root1, root2;
  7.         Scanner read = new Scanner(System.in);
  8.        
  9.         System.out.println("Enter the coefficient of x squared: ");
  10.         a = read.nextInt();
  11.        
  12.         System.out.println("Enter the coefficient of x: ");
  13.         b = read.nextInt();
  14.        
  15.         System.out.println("Enter the constant: ");
  16.         c = read.nextInt();
  17.        
  18.         discriminant = Math.pow(b,2)-(4*a*c);
  19.         root1= ((-1*b)+Math.sqrt(discriminant))/(2*a);
  20.         root2= ((-1*b)-Math.sqrt(discriminant))/(2*a);
  21.        
  22.         DecimalFormat fmt = new DecimalFormat("0.###");
  23.         System.out.println("Root1: "+ fmt.format(root1));
  24.         System.out.println("Root2: "+ fmt.format(root2));
  25.        
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement