{"id":23688,"date":"2021-10-03T20:26:55","date_gmt":"2021-10-03T14:56:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=23688"},"modified":"2021-11-22T18:33:26","modified_gmt":"2021-11-22T13:03:26","slug":"python-program-for-volume-of-pyramid","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-volume-of-pyramid\/","title":{"rendered":"Python Program for Volume of Pyramid"},"content":{"rendered":"
Given the base, height, area of a pyramid and the task is to calculate the volume of given pyramid types in python.<\/p>\n
Pyramid :<\/strong><\/p>\n A pyramid is a three-dimensional geometric shape created by connecting all of the corners of a polygon to a central apex.<\/p>\n Pyramids come in a variety of shapes and sizes. They are usually named after the type of base they have. Let’s take a look at some of the most common types of pyramids below.<\/p>\n Formulas:<\/strong><\/p>\n The Volume of square base pyramid\u00a0 = (1\/3) * (b^2) * h where b= base of the pyramid<\/p>\n h= height<\/p>\n a= area<\/p><\/blockquote>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to calculate the volume of given pyramid types in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Given the base, height, area of a pyramid and the task is to calculate the volume of given pyramid types in python. Pyramid : A pyramid is a three-dimensional geometric shape created by connecting all of the corners of a polygon to a central apex. Pyramids come in a variety of shapes and sizes. They …<\/p>\n
\nThe volume of triangular base pyramid = (1\/6) * a * b * h
\nThe Volume of pentagonal base pyramid = (5\/6) * a * b * h
\nThe volume of a hexagonal base pyramid = a * b * h<\/p>\nGiven base = 3\r\nGiven height = 5\r\nGiven area = 20<\/pre>\n
The Volume of triangular base pyramid with given base, height, area { 3 , 5 , 20 } = 49.97999999999999\r\nThe Volume of square base pyramid with given base, height, area { 3 , 5 , 20 } = 14.849999999999998\r\nThe Volume of pentagonal base pyramid with given base, height, area { 3 , 5 , 20 } = 249.0\r\nThe Volume of hexagonal base pyramid with given base, height, area { 3 , 5 , 20 } = 300<\/pre>\n
Given base = 4\r\nGiven height = 6.2\r\nGiven area = 15<\/pre>\n
The Volume of triangular base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 61.9752\r\nThe Volume of square base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 32.736000000000004\r\nThe Volume of pentagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 308.76\r\nThe Volume of hexagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 372.0<\/pre>\n
Program for Volume of Pyramid in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Give the base as static input and store it in a variable.\r\ngvn_baseval = 3\r\n# Give the height as static input and store it in another variable.\r\ngvn_heigt = 5\r\n# Give the area as static input and store it in another variable.\r\ngvn_area = 20\r\n# Calculate the volume of the triangular pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\ntriangulr_pyramidvol = (0.1666) * gvn_area * gvn_baseval * gvn_heigt\r\n# Calculate the volume of the square pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\nsqure_pyramidvol = (0.33) * gvn_baseval * gvn_baseval * gvn_heigt\r\n# Calculate the volume of the pentagonal pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\npentagonl_pyramidvol = (0.83) * gvn_area * gvn_baseval * gvn_heigt\r\n# Calculate the volume of the hexagonal pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\nhexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt\r\n# Print the volume of the triangular pyramid.\r\nprint(\"The Volume of triangular base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", triangulr_pyramidvol)\r\n# Print the volume of the square pyramid.\r\nprint(\"The Volume of square base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", squre_pyramidvol)\r\n# Print the volume of the pentagonal pyramid.\r\nprint(\"The Volume of pentagonal base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", pentagonl_pyramidvol)\r\n# Print the volume of the hexagonal pyramid.\r\nprint(\"The Volume of hexagonal base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", hexgnl_pyramidvol)\r\n<\/pre>\n
#include <iostream>\r\n#include<math.h>\r\n\r\nusing namespace std;\r\n\r\nint main() {\r\n int gvn_baseval = 3;\r\nint gvn_heigt = 5;\r\nint gvn_area = 20;\r\ndouble triangulr_pyramidvol = ( 0.1666 ) * gvn_area * gvn_baseval * gvn_heigt;\r\ndouble squre_pyramidvol = ( 0.33 ) * gvn_baseval * gvn_baseval * gvn_heigt;\r\ndouble pentagonl_pyramidvol = ( 0.83 ) * gvn_area * gvn_baseval * gvn_heigt;\r\n double hexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt;\r\ncout<<\"The Volume of triangular base pyramid with given base, height, area {\"<<gvn_baseval<< \",\"<<gvn_heigt<<\",\"<<gvn_area<<\"} = \"<<triangulr_pyramidvol<<endl;\r\ncout<<\"The Volume of square base pyramid with given base, height, area {\"<<gvn_baseval<< \",\"<<gvn_heigt<<\",\"<<gvn_area<<\"} = \"<<squre_pyramidvol<<endl;\r\ncout<<\"The Volume of pentagonal base pyramid with given base, height, area {\"<<gvn_baseval<< \",\"<<gvn_heigt<<\",\"<<gvn_area<<\"} = \"<<pentagonl_pyramidvol<<endl;\r\ncout<<\"The Volume of hexagonal base pyramid with given base, height, area {\"<<gvn_baseval<< \",\"<<gvn_heigt<<\",\"<<gvn_area<<\"} = \"<<hexgnl_pyramidvol<<endl;\r\n\r\n}<\/pre>\n
The Volume of triangular base pyramid with given base, height, area { 3 , 5 , 20 } = 49.97999999999999\r\nThe Volume of square base pyramid with given base, height, area { 3 , 5 , 20 } = 14.849999999999998\r\nThe Volume of pentagonal base pyramid with given base, height, area { 3 , 5 , 20 } = 249.0\r\nThe Volume of hexagonal base pyramid with given base, height, area { 3 , 5 , 20 } = 300<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Give the base as user input using the float(input()) function and store it in a variable.\r\ngvn_baseval = float(input(\"Enter some random number = \"))\r\n# Give the height as user input using the float(input()) function and store it in another variable.\r\ngvn_heigt = float(input(\"Enter some random number = \"))\r\n# Give the area as user input using the float(input()) function and store it in another variable.\r\ngvn_area = float(input(\"Enter some random number = \"))\r\n# Calculate the volume of the triangular pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\ntriangulr_pyramidvol = (0.1666) * gvn_area * gvn_baseval * gvn_heigt\r\n# Calculate the volume of the square pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\nsqure_pyramidvol = (0.33) * gvn_baseval * gvn_baseval * gvn_heigt\r\n# Calculate the volume of the pentagonal pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\npentagonl_pyramidvol = (0.83) * gvn_area * gvn_baseval * gvn_heigt\r\n# Calculate the volume of the hexagonal pyramid using the above given mathematical formula.\r\n# Store it in another variable.\r\nhexgnl_pyramidvol = gvn_area * gvn_baseval * gvn_heigt\r\n# Print the volume of the triangular pyramid.\r\nprint(\"The Volume of triangular base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", triangulr_pyramidvol)\r\n# Print the volume of the square pyramid.\r\nprint(\"The Volume of square base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", squre_pyramidvol)\r\n# Print the volume of the pentagonal pyramid.\r\nprint(\"The Volume of pentagonal base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", pentagonl_pyramidvol)\r\n# Print the volume of the hexagonal pyramid.\r\nprint(\"The Volume of hexagonal base pyramid with given base, height, area {\",\r\n gvn_baseval, \",\", gvn_heigt, \",\", gvn_area, \"} = \", hexgnl_pyramidvol)\r\n<\/pre>\n
Enter some random number = 4\r\nEnter some random number = 6.2\r\nEnter some random number = 15\r\nThe Volume of triangular base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 61.9752\r\nThe Volume of square base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 32.736000000000004\r\nThe Volume of pentagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 308.76\r\nThe Volume of hexagonal base pyramid with given base, height, area { 4.0 , 6.2 , 15.0 } = 372.0<\/pre>\n