C++ Exercises: Calculate the volume of a Cone
C++ Basic: Exercise-45 with Solution
Write a C++ program to calculate the volume of a cone.
Sample Solution:
C++ Code :
#include <iostream> // Including the input-output stream header file
#include <string> // Including the string header file (although it's not used in this code)
using namespace std; // Using the standard namespace
int main() // Start of the main function
{
const float pi = 3.14159; // Declaring a constant variable 'pi' to store the value of pi
float R, H, V; // Declaring variables to store the radius, height, and volume of the cone
cout << "Input Cone's radius: "; // Outputting a message to prompt the user to input the cone's radius
cin >> R; // Reading the cone's radius input from the user
cout << "Input Cone's height: "; // Outputting a message to prompt the user to input the cone's height
cin >> H; // Reading the cone's height input from the user
// Calculating the volume of the cone using the formula: V = (1/3) * π * R^2 * H
V = (1.0 / 3.0) * pi * (R * R) * H;
cout << "The volume of the cone is: " << V; // Outputting the calculated volume of the cone
return 0; // Return statement indicating successful completion of the program
}
Sample Input: 5 3
Sample Output:
Input Cone's radius: Input Cone's height: The volume of the cone is: 78.5397
Flowchart:
C++ Code Editor:
Previous: Write a language program to get the volume of a sphere with radius 6.
Next: Write a program in C++ to calculate the volume of a cylinder.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/cpp-exercises/basic/cpp-basic-exercise-45.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics