Write a C program that computes and outputs the volume of a
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)
