इस लेख में, हम समझेंगे कि जावा में द्विघात समीकरण की जड़ों की गणना कैसे करें। द्विघात समीकरण दूसरी डिग्री का बीजगणितीय व्यंजक है या दूसरे शब्दों में, इसके दो परिणाम होते हैं अर्थात वास्तविक संख्या और एक काल्पनिक संख्या।
नीचे उसी का एक प्रदर्शन है -
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
इनपुट
मान लीजिए हमारा इनपुट है -
a = 1, b = 2, c = 3
आउटपुट
वांछित आउटपुट होगा -
The roots of the quadratic equation are root_1 = -1.00+1.41i root_2 = -1.00-1.41i
एल्गोरिदम
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
उदाहरण 1
यहां, उपयोगकर्ता द्वारा एक संकेत के आधार पर इनपुट दर्ज किया जा रहा है। आप इस उदाहरण को हमारे कोडिंग ग्राउंड टूल में लाइव देख सकते हैं ।
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); } } }
आउटपुट
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
उदाहरण 2
यहां, पूर्णांक को पहले परिभाषित किया गया है, और इसके मान को एक्सेस किया जाता है और कंसोल पर प्रदर्शित किया जाता है।
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); } } }
आउटपुट
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