Created
December 16, 2020 21:59
-
-
Save anjoismysign/fa95b9dff5b39e4ae54ecbf33e7f80ea to your computer and use it in GitHub Desktop.
QuadraticEqRoots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.Math; | |
public class QuadraticEqRoots{ | |
public static int gcd(int a,int b){ | |
int res = 0; | |
while (b > 0){ | |
int temp = b; | |
b = a % b; | |
a = temp; | |
res = a; | |
} | |
return res; | |
} | |
public static void main(String []args){ | |
double a = 1; | |
double b = 8; | |
double c = -20; | |
double mcd = 0; | |
String x1Str = null; | |
String x2Str = null; | |
double D = Math.pow(b,2)-(4*a*c); | |
System.out.println(" "); | |
int[] myArray = {(int)a, (int)b, (int)c}; | |
int result = gcd(myArray[0],myArray[1]); | |
for(int i = 2; i < myArray.length; i++){ | |
result = gcd(result, myArray[i]); | |
} | |
mcd = result; | |
System.out.println("Greatest Common Divisor: " + String.valueOf(mcd)); | |
System.out.println(" "); | |
System.out.println("Discriminant: " + String.valueOf(D)); | |
System.out.println(""); | |
if (D > 0){ | |
System.out.println("Two solutions: "); | |
double x1 = (-b + Math.sqrt(D)) / (2*a); | |
double x2 = (-b - Math.sqrt(D)) / (2*a); | |
x1Str = String.valueOf("(x + " + Math.abs(x1) + ")"); | |
x2Str = String.valueOf("(x + " + Math.abs(x2) + ")"); | |
if (x1 >= 0){ | |
x1Str = "(x - " + Math.abs(x1) + ")"; | |
} | |
if (x2 >= 0){ | |
x2Str = "(x - " + Math.abs(x2) + ")"; | |
} | |
System.out.println(""); | |
System.out.println("Result: " + x1Str + " " + x2Str); | |
} | |
if (D < 0){ | |
System.out.println("No solution..."); | |
} | |
if (D == 0){ | |
System.out.println("One solution: "); | |
double x1 = (-b + Math.sqrt(D)) / (2*a); | |
double x2 = (-b - Math.sqrt(D)) / (2*a); | |
x1Str = String.valueOf("(x + " + Math.abs(x1) + ")"); | |
x2Str = String.valueOf("(x + " + Math.abs(x2) + ")"); | |
if (x1 >= 0){ | |
x1Str = "(x - " + Math.abs(x1) + ")"; | |
} | |
if (x2 >= 0){ | |
x2Str = "(x - " + Math.abs(x2) + ")"; | |
} | |
if (x1 == 0){ | |
x1Str = "(x)"; | |
} | |
if (x2 == 0){ | |
x2Str = "(x)"; | |
} | |
System.out.println(""); | |
System.out.println("Result: " + x1Str + " " + x2Str + " (" + mcd + ")"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment