Okay, so I'm a beginner at C++. I have this homework problem:
Write a C++ program that computes and outputs the volume of a cone, given the diameter of its base and its height. The formula for computing the cone's volume is:
1/3 PI * Radius ^2 * Height.
I used the <cmath>, and <iostream> and all of that. Should I use <iomanip>? Do I have to use setprecision anywhere?
EDIT: Sorry, this really should be in the beginner section :X
EDIT:
Here's the code:
//This program will compute the volume of a cone given the diameter of its base and its height.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const double PI = 3.14159; //Value of PI, and it cannot be changed.
//This program will compute the volume of a cone given the diameter of its base and its height.
#include <iostream>
#include <cmath>
usingnamespace std;
constdouble PI = 3.1415926;//Value of PI and it cannot be modified
int main()
{
double diameter, radius, volume;
int height;
cout << "Enter the diameter of the cone's base." << endl;
cin >> diameter;
radius = diameter / 2.0; //Calculates the radius of the base.
cout << "Enter the height of the cone." << endl;
cin >> height;
volume = ((1/3.0) * PI) * pow (radius, 2.0) * height; //Calculates the volume of the cone.
cout << "The volume of the cone is " << volume << endl;
return 0;
}
If any of you wanted to try it out and maybe see if it works correctly for you too, that would be greatly appreciated!
#include <iostream>
#include <cmath>
usingnamespace std;
constdouble PI = 3.1415926;//Value of PI and it cannot be modified
int main()
{
double volume(double radius, double height);
double diameter, height;
cout << "Enter the diameter of the cone's base." << endl;
cin >> diameter;
cout << "Enter the height of the cone." << endl;
cin >> height;
cout << "The volume of the cone is " << volume(diameter/2,height) << endl;
return 0;
}
double volume(double radius, double height)
{
return pow(radius, 2.0)/3.0 * height * PI;
}