fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static double sqrt(double n) {
  6. double x, y, eps = 0.000001;
  7. x = n;
  8. y = 1.0;
  9. while(x - y > eps) {
  10. x = (x + y) / 2;
  11. y = n / x;
  12. }
  13. return x;
  14. }
  15.  
  16. public static void NatureRootsQuadraticEquation( double a, double b, double c ) {
  17.  
  18. double S, P, Delta, x1, x2;
  19.  
  20. S = -b/a;
  21. P = c/a;
  22. Delta = b*b - 4*a*c;
  23. System.out.println("\nVerify: ");
  24. if(Delta < 0) {
  25. System.out.println("Imaginary");
  26. } else {
  27. x1 = (-b-sqrt(Delta))/(2*a);
  28. x2 = (-b+sqrt(Delta))/(2*a);
  29. System.out.printf("x1 = %.2f\n",x1);
  30. System.out.printf("x2 = %.2f\n",x2);
  31. if( S >= 0 ) {
  32. if(P > 0) {
  33. System.out.println("x1 > 0 positive, x2 > 0 positive");
  34. } else if(P < 0){
  35. System.out.println("x1 < 0 negative, x2 > 0 negative, |x1| < |x2|");
  36. } else {
  37. System.out.println("x1 = 0 ,x2 > 0");
  38. }
  39. //the S < 0 negative
  40. } else {
  41. if(P > 0) {
  42. System.out.println("x1 < 0 positive, x2 < 0 negative");
  43. } else if(P < 0){
  44. System.out.println("x1 < 0 negative, x2 > 0 negative, |x1| > |x2|");
  45. } else {
  46. System.out.println("x1 = 0 ,x2 < 0");
  47. }
  48. }
  49. }
  50. }
  51.  
  52. public static void main( String args[] ) {
  53.  
  54. double a, b, c;
  55. Scanner keyboard = new Scanner( System.in );
  56. if(args.length > 1){
  57. a = Double.parseDouble(args[0]);
  58. b = Double.parseDouble(args[1]);
  59. c = Double.parseDouble(args[2]);
  60. } else {
  61. System.out.println("Give A, B, C for Ax^2 + Bx + C = 0: ");
  62. a = keyboard.nextDouble();
  63. b = keyboard.nextDouble();
  64. c = keyboard.nextDouble();
  65. }
  66. System.out.printf("The nature of the Roots Quadratic Equation: %.2fx^2 + %.2fx + %.2fc = 0", a, b, c);
  67. NatureRootsQuadraticEquation(a,b,c);
  68. }
  69. }
  70.  
Success #stdin #stdout 0.16s 60128KB
stdin
1 4 -2
stdout
Give A, B, C for Ax^2 + Bx + C = 0: 
The nature of the Roots Quadratic Equation: 1.00x^2 + 4.00x + -2.00c = 0
Verify: 
x1 = -4.45
x2 = 0.45
x1 < 0 negative, x2 > 0 negative, |x1| > |x2|