The Program in C++ to Calculate the Volume of a Cone is given below:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
const float pi = 3.14159;
float R, H, V;
cout << "Hello Codeauri Family, Please Input the Cone's radius here: ";
cin >> R;
cout << "Similarly, input the Cone's height as well: ";
cin >> H;
// Cone's volume.
V = (1.0/3.0)*pi*(R*R)*H;
cout << "Wow,The volume of the cone is: " << V ;
return 0;
}
Output:
Hello Codeauri Family, Please Input the Cone’s radius here: 7
Similarly, input the Cone’s height as well: 8
Wow,The volume of the cone is: 410.501
Pro-Tips💡
The first line #include <iostream>
is a preprocessor directive that includes the iostream standard file.
This file contains functions that perform standard input and output operations, such as reading input from the keyboard and displaying output on the screen.
The second line #include <string>
is a preprocessor directive that includes the string standard file.
This file contains functions that perform string operations, such as creating, modifying, and comparing strings. However, in this code, it is not used.
The line using namespace std;
is a namespace declaration that allows the program to use the names of functions and objects from the standard C++ library without having to qualify them with the std:: prefix.
The line int main ()
is the main function of the program, where the program execution starts.
The code inside the curly brackets {} is executed when the program runs.
The line const float pi = 3.14159;
declares a constant variable pi and assigns it the value of 3.14159. This value is used in the formula to calculate the volume of the cone.
The line float R, H, V;
declares three variables R, H, and V of type float. R and H will be used to store the input values for the radius and height of the cone, respectively.
V will be used to store the calculated volume of the cone.
The lines cout << "Hello Codeauri Family, Please Input the Cone's radius here: ";
and cout
oaicite:{“index”:0,”invalid_reason”:”Malformed citation << “Similarly, input the Cone’s height as well: “;are used to display messages on the screen asking the user to input the values for the radius and height of the cone.
\n\nThe lines
cin >> R;and
cin >>”} H;
are used to read the input values for the radius and height of the cone, respectively.
The line // Cone's volume.
is a comment that describes the next line of code, which is the calculation of the volume of the cone.
The line V = (1.0/3.0)*pi*(R*R)*H;
calculates the volume of the cone using the formula (1/3) * pi * r^2 * h.
The value of pi is the constant value assigned earlier, and the values of R and H are the input values read from the user. The result is stored in the variable V.
The line cout << "Wow,The volume of the cone is: " << V ;
is used to display the calculated volume of the cone on the screen.
Finally, the line return 0;
is used to indicate that the program has been completed successfully and return control to the operating system.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.