Persamaan Kuadrat

Assalamualaikum, .
Kali ini saya akan membagikan tentang aritmatika, yaitu persamaan kuadrat pada pemrograman java, .
Silahkan dicoba, . 

import java.util.Scanner;

public class QuadraticEquation {
  
    public static void main(String[] iriahus) {
        @SuppressWarnings("resource")
        Scanner s = new Scanner(System.in);
      
        System.out.print("Enter a, b, and c: ");
        double a = s.nextDouble();
        double b = s.nextDouble();
        double c = s.nextDouble();
      
        double D = b * b - 4 * a * c;
        double SqrtD = Math.sqrt(D);
        double x1 = (-b + SqrtD) / (2 * a);
        double x2 = (-b - SqrtD) / (2 * a);
      
        if (D == 0) {
            System.out.println("Discriminant is 0.\n" +
                    "The roots are twins, that is " + x1);
        } else if (D > 0) {
            System.out.println("Discriminant is greater than 0.\n" +
                    "The roots are " + x1 + " and " + x2);
        } else {
            System.out.println("Discriminant is less than 0.\n" +
                    "The equation has no real roots.");
        }
    }

}

Semoga bermanfaat, . .

Subscribe to receive free email updates:

0 Response to "Persamaan Kuadrat"

Post a Comment