Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java หารากของสมการกำลังสอง (Quadratic Equation)

ในบทความนี้ เราจะเรียนรู้วิธีหารากของสมการกำลังสองในภาษา Java สมการกำลังสองมีรูปแบบ ax² + bx + c = 0 มีรากได้ 2 ราก (อาจเป็นจริงหรือจินตภาพ)

กรณีของราก (Discriminant: Δ = b² - 4ac)

  • Δ < 0: รากเป็นจำนวนเชิงซ้อน (Complex) - ไม่มีรากจริง
  • Δ = 0: รากจริงทั้งสองเท่ากัน (Double root)
  • Δ > 0: รากจริงต่างกัน 2 ค่า

ตัวอย่างการทำงาน

อินพุต:

a = 1, b = 2, c = 3

เอาต์พุต:

root_1 = -1.00+1.41i
root_2 = -1.00-1.41i

อัลกอริทึม

  1. เริ่มต้นโปรแกรม
  2. ประกาศตัวแปร double: a, b, c, root_1, root_2, discriminant
  3. รับค่า a, b, c จากผู้ใช้ หรือกำหนดค่าไว้ล่วงหน้า
  4. คำนวณ discriminant = b² - 4ac
  5. ตรวจสอบเงื่อนไข discriminant และคำนวณรากตามสูตรกำลังสอง
  6. แสดงผลลัพธ์
  7. จบโปรแกรม

ตัวอย่างที่ 1: รับค่าจากผู้ใช้

import java.util.Scanner;

public class QuadraticEquation {
    public static void main(String[] args) {
        double a, b, c, root_1, root_2, discriminant;
        double realPart, imaginaryPart;

        System.out.println("Required packages have been imported");
        Scanner my_scanner = new Scanner(System.in);
        System.out.println("A scanner object has been defined");

        System.out.print("Enter the value of a: ");
        a = my_scanner.nextDouble();
        System.out.print("Enter the value of b: ");
        b = my_scanner.nextDouble();
        System.out.print("Enter the value of c: ");
        c = my_scanner.nextDouble();

        discriminant = b * b - 4 * a * c;

        if (discriminant > 0) {
            root_1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            root_2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
        } else if (discriminant == 0) {
            root_1 = root_2 = -b / (2 * a);
            System.out.format("root_1 = root_2 = %.2f", root_1);
        } else {
            realPart = -b / (2 * a);
            imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
            System.out.println("The roots of the quadratic equation are");
            System.out.printf("root_1 = %.2f+%.2fi", realPart, imaginaryPart);
            System.out.printf("\nroot_2 = %.2f-%.2fi", realPart, imaginaryPart);
        }
    }
}

ตัวอย่างที่ 2: กำหนดค่าไว้ล่วงหน้า

public class QuadraticEquation {
    public static void main(String[] args) {
        double a = 1, b = 2, c = 3;
        double root_1, root_2, discriminant;
        double realPart, imaginaryPart;

        System.out.println("The three numbers are defined as " + a + ", " + b + " and " + c);

        discriminant = b * b - 4 * a * c;

        if (discriminant > 0) {
            root_1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            root_2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
        } else if (discriminant == 0) {
            root_1 = root_2 = -b / (2 * a);
            System.out.format("root_1 = root_2 = %.2f", root_1);
        } else {
            realPart = -b / (2 * a);
            imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
            System.out.println("The roots of the quadratic equation are");
            System.out.printf("root_1 = %.2f+%.2fi", realPart, imaginaryPart);
            System.out.printf("\nroot_2 = %.2f-%.2fi", realPart, imaginaryPart);
        }
    }
}

ทั้งสองตัวอย่างให้ผลลัพธ์รากจินตภาพเพราะ discriminant = 4 - 12 = -8 (< 0)