C++ Exercises: Calculate the volume of a cube
C++ Basic: Exercise-15 with Solution
Write a C++ program that calculates the volume of a cube.
Visual Presentation:
Sample Solution:
C++ Code :
#include <iostream> // Including the input-output stream header file
using namespace std; // Using the standard namespace
int main() // Start of the main function
{
int sid1; // Declaring an integer variable sid1 to store the side length of the cube
float volcu; // Declaring a floating-point variable volcu to store the volume of the cube
cout << "\n\n Calculate the volume of a cube :\n"; // Outputting a message indicating the calculation of cube volume
cout << "---------------------------------------\n"; // Outputting a separator line
cout << " Input the side of a cube : "; // Prompting the user to input the side length of the cube
cin >> sid1; // Taking input for the side length from the user
// Calculating the volume of the cube using the formula: side * side * side
volcu = (sid1 * sid1 * sid1);
cout << " The volume of a cube is : "<< volcu << endl; // Displaying the calculated volume of the cube
cout << endl; // Outputting a blank line for better readability
return 0; // Returning 0 to indicate successful program execution
} // End of the main function
Sample Output:
Calculate the volume of a cube : --------------------------------------- Input the side of a cube : 5 The volume of a cube is : 125
Flowchart:
C++ Code Editor:
Previous: Write a program in C++ to calculate the volume of a sphere.
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-15.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics