Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để tìm tất cả các gốc của một phương trình bậc hai

Trong bài này, chúng ta sẽ hiểu cách tính nghiệm nguyên của phương trình bậc hai trong Java. Phương trình bậc hai là một biểu thức đại số của bậc hai hay nói cách khác, nó có hai kết quả là số thực và số ảo.

Dưới đây là một minh chứng về điều tương tự -

Cho phương trình bậc hai có dạng ax2 + bx + c -

There are three cases:
b2 < 4*a*c - The roots are not real i.e. they are complex
b2 = 4*a*c - The roots are real and both roots are the same.
b2 > 4*a*c - The roots are real and both roots are different

Đầu vào

Giả sử đầu vào của chúng tôi là -

a = 1, b = 2, c = 3

Đầu ra

Đầu ra mong muốn sẽ là -

The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i

Thuật toán

Step1- Start
Step 2- Declare 6 double values: a, b, c, root_1, root_2, quadratic_equation
Step 3- Prompt the user to enter a,b,c double values/ define the double values
Step 4- Read the values
Step 5- In a for loop, check if the value of quadratic_equation variable is greater than 0, and if
true, use quadric formula to find the value, and assign it to a variable.
Step 6- Display the result
Step 7- Stop

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để tìm tất cả các gốc của một phương trình bậc hai .

import java.util.Scanner;
public class QuadraticEquation {
   public static void main(String[] args) {
      double a, b, c, root_1, root_2, quadratic_equation;
      double real_number, imaginary_number;
      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();
      quadratic_equation = b*b - 4*a*c ;
      if (quadratic_equation > 0) {
         root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a);
         root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a);
         System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
      }
      else 
      if (quadratic_equation == 0) {
         root_1 = root_2 = -b / (2 * a);
         System.out.format("root_1 = root_2 = %.2f;", root_1);
      }
      else {
         real_number = -b / (2 * a);
         imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a);
         System.out.println("The roots of the quadratic equation are");
         System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number);
         System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number);
      }
   }
}

Đầu ra

Required packages have been imported
A scanner object has been defined
Enter the value of a : 1
Enter the value of b : 2
Enter the value of c : 3
The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class QuadraticEquation {
    public static void main(String[] args) {
      double a, b, c, root_1, root_2, quadratic_equation;
      double real_number, imaginary_number;
      a = 1;
      b = 2;
      c = 3;
      System.out.println("The three numbers are defined as " +a +", " +b +" and " +c);
      quadratic_equation = b*b - 4*a*c ;
      if (quadratic_equation > 0) {
         root_1 = (-b + Math.sqrt(quadratic_equation)) / (2 * a);
         root_2 = (-b - Math.sqrt(quadratic_equation)) / (2 * a);
         System.out.format("root_1 = %.2f and root_2 = %.2f", root_1, root_2);
      }
      else 
      if (quadratic_equation == 0) {
         root_1 = root_2 = -b / (2 * a);
         System.out.format("root_1 = root_2 = %.2f;", root_1);
      }
      else {
         real_number = -b / (2 * a);
         imaginary_number = Math.sqrt(-quadratic_equation) / (2 * a);
         System.out.println("The roots of the quadratic equation are");
         System.out.printf("root_1 = %.2f+%.2fi", real_number, imaginary_number);
         System.out.printf("\nroot_2 = %.2f-%.2fi", real_number, imaginary_number);
      }
   }
}

Đầu ra

The three numbers are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are
root_1 = -1.00+1.41i
root_2 = -1.00-1.41i