In: Computer Science
Write a program in C++ that solves this problem
Calculate the area and volume of a sphere problem.
Inside a for loop,
vary the radius from 10 to 40 with a step or increment of 5
and calculate the area and volume
Your radius will be equal to your loop counter.
All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma.
Print the radius, area, and volume in a table format, with 3 columns. Note: you don’t have to print grid lines.
Print column headings above the loop and all the calculations inside the loop. The column headings would just be Radius, Area, and Volume. Right justify your data under your column headings
Below the loop, print the sum of the radii, the sum of the areas, and the sum of the volumes. The sum of the areas and the sum of the volumes should have 2 decimal places and commas. The sum of the radii should have zero decimal places.
Answer must be formatted like the following
Radius............Area...............Volume.
10 1,256.64 4,188.79
15 2,827.43 14,137.15
20 5,026.54 33,510.29
25 7,853.97 65,449.79
30 11,309.72 113,097.24
35 15,393.79 179,594.23
40 20,106.18 268,082.35
Totals:
Radius............Area...............Volume..
175 63,774.28 678,059.84
Radius Area Volume
10 1,256.64 4,188.79
15 2,827.43 14,137.17
20 5,026.55 33,510.32
25 7,853.98 65,449.85
30 11,309.73 113,097.34
35 15,393.80 179,594.38
40 20,106.19 268,082.57
Sum of Radii: 175
Sum of Areas: 63,774.33
Sum of Volumes: 678,060.41
#include<iostream>
#include<iomanip>
using namespace std;
//structure to print comma after thousand place
struct comma_facet : public std::numpunct<char>
{ protected: string do_grouping() const { return "\003" ; }
};
//driver program
int main()
{
int i=10,sum=0;
cout.imbue( locale( cout.getloc(), new
comma_facet ) ) ;
double area,volume,sumarea=0,sumvolume=0;
//first part output
//display the heading
cout<<endl<<"Radius.........Area........Volume";
//loop to compute the area and volume of sphere
starting from radius 10 to 40
//step value 5
for(i=10;i<=40;i+=5)
{
area = 4 * 3.14159265 *
i*i;//compute area
volume = ((double)4/3) * 3.14159265
* (i*i*i);//compute volume
sum=sum+i;//find the radius
totals
sumarea=sumarea+area;//compute the
area totals
sumvolume=sumvolume+volume;//compute the volume totals
//display the details
cout<<endl<<i<<"\t
"<<fixed<<setprecision(2)<<area<<"\t
"<<fixed<<setprecision(2)<<volume;
}
//display the totals
cout<<endl<<"Totals : ";
cout<<endl<<"Radius.........Area........Volume";
cout<<endl<<sum<<"\t
"<<sumarea<<"\t"<<sumvolume;
//second part output
sumarea=0;sumvolume=0;//set sumaea and sumvolume to
0
cout<<endl<<"Radius Area
Volume";
//loop to compute the area and volume of sphere
starting from radius 10 to 40
//step value 5
for(i=10;i<=40;i+=5)
{
area = 4 * 3.14159265 * i*i;
volume = ((double)4/3) * 3.14159265
* (i*i*i);
sum=sum+i;//find the radius
totals
sumarea=sumarea+area;//compute the
area totals
sumvolume=sumvolume+volume;//compute the volume totals
//display the details
cout<<endl<<i<<"\t
"<<fixed<<setprecision(2)<<area<<"\t
"<<fixed<<setprecision(2)<<volume;
}
//display the totals
cout<<endl<<"Sum of Radii :
"<<sum;
cout<<endl<<"Sum of Areas :
"<<sumarea;
cout<<endl<<"Sum of Volumes :
"<<sumvolume;
}
output