三角形、長方形、円の周囲と面積を計算する Python プログラム.

1897 ワード

三角形の周囲と面積



高さ(h)、長さ(l)、幅(w)の三角形の周囲と面積を計算する数式は次のとおりです.p= h+l+w
a=1/2(l*w)
したがって、Pythonでは次のように実行できます

length=float(input("Enter length : "))
width =float(input("Enter width : "))
height=float(input("Enter height: "))

print("the perimetre of the triangle is : " +str(length+width+height))
print("the area of the triangle is : " +str(1/2*(length*width)))


出力は次のようになります
Enter length : 2
Enter width : 3
Enter height: 4
the perimeter of the triangle is : 9.0
the area of the triangle is : 3.0

長方形の周囲と面積



長さ (l) と幅 (w) の Rectangle の周長と面積を計算する数式は次のとおりです.
p= 2l + 2w
= l*w
そしてコードは

length=float(input("Enter length : "))
width =float(input("Enter width : "))

print("the perimeter of the rectangle is : "+str(2*length+2*width))
print("the area of the rectangle is : "+str(length*width))


出力は次のようになります
Enter length : 2
Enter width : 3
the perimeter of the rectangle is : 10.0
the area of the rectangle is: 6.0

円の周囲と面積



半径 (r) の円の周長と面積を計算する数式は次のとおりです.p= 2*PI*r
a=PI*r**2
したがって、Pythonでは次のように実行できます

from math import pi
r=float(input("Enter radius : "))
print("the Perimetre of the circle with radius "+str(r)+" is : " +str(2*pi*r))
print("the area of the circle with radius "+str(r)+" is : " +str(pi*r**2))


出力は次のようになります
Enter radius : 3
the Perimeter of the circle with radius 3.0 is : 18.84955592153876
the area of the circle with radius 3.0 is : 28.274333882308138