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

โปรแกรม Java เพื่อค้นหารากทั้งหมดของสมการกำลังสอง


ในบทความนี้ เราจะเข้าใจวิธีการคำนวณรากของสมการกำลังสองใน Java สมการกำลังสองคือนิพจน์พีชคณิตของดีกรีที่สองหรือกล่าวอีกนัยหนึ่ง มีสองผลลัพธ์ ได้แก่ จำนวนจริงและจำนวนจินตภาพ

ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -

จากสมการกำลังสองของรูปแบบ ax2 + bx + c −

มีสามกรณี:b2 <4*a*c - รูตไม่มีจริง นั่นคือ พวกมันซับซ้อนb2 =4*a*c - รูตเป็นของจริงและรูตทั้งสองเหมือนกัน b2> 4*a*c - รากเป็นของจริงและรากทั้งสองต่างกัน

ป้อนข้อมูล

สมมติว่าข้อมูลที่เราป้อนคือ −

a =1, b =2, c =3

ผลผลิต

ผลลัพธ์ที่ต้องการจะเป็น −

รากของสมการกำลังสอง areroot_1 =-1.00+1.41iroot_2 =-1.00-1.41i

อัลกอริทึม

ขั้นตอนที่ 1- StartStep 2- ประกาศค่าคู่ 6 ค่า:a, b, c, root_1, root_2, quadratic_equationStep 3- แจ้งให้ผู้ใช้ป้อนค่า a,b,c double/กำหนดค่า double valuesStep 4- อ่านค่าขั้นตอนที่ 5- ใน for loop ให้ตรวจสอบว่าค่าของตัวแปร quadratic_equation มากกว่า 0 หรือไม่ และ iftrue ให้ใช้สูตรสมการกำลังสองเพื่อค้นหาค่าและกำหนดค่าให้กับตัวแปร ขั้นตอนที่ 6- แสดงผลลัพธ์ขั้นตอนที่ 7- หยุด

ตัวอย่างที่ 1

ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์ของเรา โปรแกรม Java เพื่อค้นหารากทั้งหมดของสมการกำลังสอง .

<ก่อน>นำเข้า java.util.Scanner; QuadraticEquation คลาสสาธารณะ { โมฆะคงที่สาธารณะหลัก (สตริง [] args) { double a, b, c, root_1, root_2, quadratic_equation; real_number สองเท่า, imaginary_number; System.out.println("นำเข้าแพ็คเกจที่จำเป็นแล้ว"); สแกนเนอร์ my_scanner =สแกนเนอร์ใหม่ (System.in); System.out.println("วัตถุสแกนเนอร์ถูกกำหนดแล้ว "); System.out.print("ป้อนค่าของ :"); a =my_scanner.nextDouble(); System.out.print("ป้อนค่า b :"); b =my_scanner.nextDouble(); System.out.print("ป้อนค่าของ 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 และ 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); } อื่น ๆ { real_number =-b / (2 * a); imaginary_number =Math.sqrt(-quadratic_equation) / (2 * a); System.out.println("รากของสมการกำลังสองคือ"); System.out.printf("root_1 =%.2f+%.2fi", real_number, imaginary_number); System.out.printf("\nroot_2 =%.2f-%.2fi", real_number, imaginary_number); } }}

ผลลัพธ์

นำเข้าแพ็คเกจที่ต้องการแล้ว มีการกำหนดวัตถุสแกนเนอร์แล้วป้อนค่า a :1ป้อนค่า b :2ป้อนค่าของ c :3รากของสมการกำลังสอง areroot_1 =-1.00+1.41iroot_2 =-1.00-1.41i

ตัวอย่างที่ 2

ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล

<ก่อน> QuadraticEquation คลาสสาธารณะ { โมฆะคงที่สาธารณะหลัก (สตริง [] args) { double a, b, c, root_1, root_2, quadratic_equation; real_number สองเท่า, imaginary_number; ก =1; ข =2; ค =3; System.out.println("ตัวเลขทั้งสามถูกกำหนดเป็น " +a +", " +b +" และ " +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 และ 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); } อื่น ๆ { real_number =-b / (2 * a); imaginary_number =Math.sqrt(-quadratic_equation) / (2 * a); System.out.println("รากของสมการกำลังสองคือ"); System.out.printf("root_1 =%.2f+%.2fi", real_number, imaginary_number); System.out.printf("\nroot_2 =%.2f-%.2fi", real_number, imaginary_number); } }}

ผลลัพธ์

ตัวเลขสามตัวถูกกำหนดเป็น 1.0, 2.0 และ 3.0 รากของสมการกำลังสอง areroot_1 =-1.00+1.41iroot_2 =-1.00-1.41i