Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Bhaskara's formula #422

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
37 changes: 37 additions & 0 deletions src/main/java/com/williamfiset/algorithms/math/Bhaskara.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.example;

public class Bhaskara {
public static void calculateRoots(double a, double b, double c) {

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


// Check if the discriminant is negative, zero, or positive
if (discriminant < 0) {
// No real roots
System.out.println("There are no real roots.");

} else if (discriminant == 0) {
// One real root
double x = -b / (2 * a);
System.out.println("There is one real root: " + x);

} else {
// Two real roots
double x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double x2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("There are two real roots: x1 = " + x1 + ", x2 = " + x2);
}
}

public static void main(String[] args) {

// Example using formula ax²+bx+c

double a = 2;
double b = 4;
double c = 2;
calculateRoots(a, b, c); // Calling the Bhaskara method
}
}
Empty file added startup
Empty file.