Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรมคำนวณปริมาตรพีระมิดใน C++


ให้ด้านต่างๆ ขึ้นอยู่กับประเภทฐานของพีระมิด งานคือคำนวณปริมาตรพีระมิด

พีระมิดเป็นรูป 3 มิติ ผิวภายนอกเป็นสามเหลี่ยมรวมที่จุดเดียวกันทำให้ขอบคม ปริมาตรขึ้นอยู่กับประเภทฐาน

มีประเภทฐานต่างๆ −

Triangular − ฐานเป็นสามเหลี่ยม ปริมาตร = (1/6) * a * b * h

Square − ฐานเป็นสี่เหลี่ยมจัตุรัส ปริมาตร = (1/3) * (b²) * h

Pentagonal − ฐานเป็นห้าเหลี่ยม ปริมาตร = (5/6) * a * b * h

Hexagonal − ฐานเป็นหกเหลี่ยม ปริมาตร = a * b * h

ตัวอย่าง

Input-: a=4 b=2 h=10 Output-: Volume of pyramid with triangular base is 13.328   Volume of pyramid with square base is 13.2   Volume of pyramid with pentagonal base is 66.4   Volume of pyramid with hexagonal base is 80

ภาพพีระมิดฐานสี่เหลี่ยมจัตุรัส

โปรแกรมคำนวณปริมาตรพีระมิดใน C++

อัลกอริทึม

Start Step 1 -> Declare function to find the volume of triangular pyramid   float volumeTriangular(int a, int b, int h)     Declare variable float volume = (0.1666) * a * b * h     return volume step 2 -> Declare Function to find the volume of square pyramid   float volumeSquare(int b, int h)     declare and set float volume = (0.33) * b * b * h     return volume Step 3 -> Declare Function to find the volume of pentagonal pyramid   float volumePentagonal(int a, int b, int h)     declare and set float volume = (0.83) * a * b * h     return volume Step 4 -> Declare Function to find the volume of hexagonal pyramid   float volumeHexagonal(int a, int b, int h)     declare and set float volume = a * b * h     return volume Step 5 -> In main()   Declare variables as int b = 2, h = 10, a = 4   Call volumeTriangular(a, b, h)   Call volumeSquare(b,h)   Call volumePentagonal(a, b, h)   Call volumeHexagonal(a, b, h) Stop

ตัวอย่าง

#include <bits/stdc++.h> using namespace std; // Function to find the volume of triangular pyramid float volumeTriangular(int a, int b, int h){   float volume = (0.1666) * a * b * h;   return volume; } // Function to find the volume of square pyramid float volumeSquare(int b, int h){   float volume = (0.33) * b * b * h;   return volume; } // Function to find the volume of pentagonal pyramid float volumePentagonal(int a, int b, int h){   float volume = (0.83) * a * b * h;   return volume; } // Function to find the volume of hexagonal pyramid float volumeHexagonal(int a, int b, int h){   float volume = a * b * h;   return volume; } int main(){   int b = 2, h = 10, a = 4;   cout << "Volume of pyramid with triangular base is "<<volumeTriangular(a, b, h)<<endl;   cout << "Volume of pyramid with square base is "<<volumeSquare(b, h)<< endl;   cout << "Volume of pyramid with pentagonal base is "<<volumePentagonal(a, b, h)<< endl;   cout << "Volume of pyramid with hexagonal base is "<<volumeHexagonal(a, b, h);   return 0; }

ผลลัพธ์

Volume of pyramid with triangular base is 13.328 Volume of pyramid with square base is 13.2 Volume of pyramid with pentagonal base is 66.4 Volume of pyramid with hexagonal base is 80