Circle and sphere calculator


/ Published in: Python
Save to your folder(s)

A simple program to calculate the area and perimeter of a circle and to calculate the area and volume of a sphere.
Source is commented,


Copy this code and paste it in your HTML
  1. #Code by Tharix @ Snipplr
  2. #Importing modules
  3. import math
  4.  
  5. print("Circle/Sphere calculator")
  6. print("Note: Pi is equal to",math.pi)
  7.  
  8. #Getting the radius as input
  9. r = int(input("Radius: "))
  10.  
  11. #Calculations based of the radius
  12. d = r*2
  13. #_circle is the calculation, where pi is also calculated
  14. PCirclePi = math.pi*r*2
  15. SCirclePi = math.pi*r**2
  16. #_npi is the calculation, where pi (as a symbol) is left in
  17. PCircleNPi = r*2
  18. SCircleNPi = r**2
  19. #Sphere area
  20. SSpherePi = 4*math.pi*(r**2)
  21. SSphereNPi = 4*(r**2)
  22. #Sphere volume
  23. VSpherePi = 4/3*math.pi*(r**3)
  24. VSphereNPi = (r**3)
  25.  
  26. #Output
  27. print(" ")
  28. #Diameter
  29. print("d =",d)
  30. #Circle perimeter
  31. print("P =","pi*{}".format(PCircleNPi),"or",PCirclePi)
  32. #Circle area
  33. print("Scircle =","pi*{}".format(SCircleNPi),"or",SCirclePi)
  34. #Sphere area
  35. print("Ssphere =","4*pi*{}".format(SSphereNPi)+"*{}".format(SCircleNPi),"or",SSpherePi)
  36. #Sphere volume
  37. print("Vsphere =","4/3*pi*{}".format(VSphereNPi),"or",VSpherePi)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.