Write a C program that computes and outputs the volume of a

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 i (1/3) pi * Radius^2 * Height Be sure to use proper formatting and appropriate comments in your code. The out should be labeled clearly.

Solution

C++ code:

#include <iostream>

#include <cmath>
#include <math.h>/*Including math directory*/

using namespace std;

int main(){
/*declaring height,radius and volume*/
float height;
float radius;
float volume;


cout<<\"Enter height:\"<<endl;/*asking for height*/
cin>>height;/*Stroring the height*/
cout<<\"Enter radius:\"<<endl;/*asking for radius*/
cin>>radius;/*Stroring radius*/

if (height <= 0 || radius <= 0) {/*checks for valid input*/
// not a valid solution
cout<<\"Not a valid input\";
}
else {/*if the entered input is valid then calculates the volume and displays it*/
volume=(1.0/3.0)*radius*radius*height*M_PI;
cout<<\"Volume is \"<<volume<<endl;
}

return 0;
}

output console:

Enter height: 2 Enter radius: 4 Volume is 33.5103

Exit code: 0 (normal program termination)

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site