二次方程式を解くPythonプログラム

Python

二次方程式の標準形式は次のとおりです。

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0

ソースコード

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

出力

Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)

をインポートしました cmath 複素平方根を実行するモジュール。 まず、判別式を計算してから、2次方程式の2つの解を見つけます。

の値を変更できます Ab そして c 上記のプログラムで、このプログラムをテストします。



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です