Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Java-Assignment-005.iml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
43 changes: 43 additions & 0 deletions src/Quadratic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Scanner;

public class Quadratic {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);


// Prompt and validate input
double a = getInput(scanner, "a");
double b = getInput(scanner, "b");
double c = getInput(scanner, "c");

// Calculate discriminant
double split = b * b - 4 * a * c;

// Check for different roots
if (split > 0) {
// Two real roots
double root1 = (-b + Math.sqrt(split)) / (2.0 * a);
double root2 = (-b - Math.sqrt(split)) / (2.0 * a);
System.out.printf("The roots are: %.2f and %.2f\n", root1, root2);
} else if (split == 0) {
// One real root
double root = -b / (2.0 * a);
System.out.printf("The root is: %.2f\n", root);
} else {
// No real roots
System.out.println("The equation has no real roots.");
}

scanner.close(); // Close Scanner
}

private static double getInput(Scanner scanner, String name) {
System.out.printf("Enter input for %s :", name);
while (!scanner.hasNextDouble()) {
System.out.printf("Invalid input for %s. Please enter an valid number: ", name);
scanner.next(); // Clear invalid input
}
return scanner.nextDouble();
}
}