In this post, we will learn how to calculate the volume of a pyramid using C Programming language.
The Volume of the Pyramid is found using the formula V = (⅓)Bh, where ‘B’ is the base area and ‘h’ is the height of the pyramid.
- Volume = ⅓ x Base x Height
We will be using the above formula in our program to compute the volume of the pyramid.
So, without further ado, let’s begin this tutorial.
C Program to Calculate Volume of Pyramid
// C Program to Calculate Volume of Pyramid #include <stdio.h> int main(){ int base, height; float volume; // Asking for input printf("Enter the base of the pyramid: "); scanf("%d", &base); printf("Enter the height of the pyramid: "); scanf("%d", &height); // Calculating volume volume = (0.33) * base * height; // Displaying output printf("Volume of sphere: %.2f", volume); return 0; }
Output
Enter the base of the pyramid: 7
Enter the height of the pyramid: 15
Volume of sphere: 34.65
How Does This Program Work ?
int base, height; float volume;
In this program, we have declared two int data type variables and one float data type variable named base, height and volume respectively.
// Asking for input printf("Enter the base of the pyramid: "); scanf("%d", &base); printf("Enter the height of the pyramid: "); scanf("%d", &height);
Then, the user is asked to enter the base and height of the pyramid.
// Calculating volume volume = (0.33) * base * height;
We calculate the volume using the formula V = (⅓) x Base x Height
// Displaying output printf("Volume of sphere: %.2f", volume);
Finally, the result is printed using the printf() function.
Conclusion
I hope after going through this post, you understand how to calculate the volume of a pyramid using C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read:
why I can’t use =))))
volume = ( 1 / 3 ) * acreage * height;