Formatted Table : Write a program in C++ to print a small formatted table showin
ID: 3847135 • Letter: F
Question
Formatted Table: Write a program in C++ to print a small formatted table showing the radius, diameter, circumference, and area of two circles according to the following specifications:
Prompt the user to enter the radius of two circles (in cm).
Display 3 tables. Each table should consist of the table number, the heading, and the values of radius, diameter, circumference, and area using the specified formatting.Table 1: Use the following formatting:
Use the table heading shown in the sample output
Left justification
Fixed format
3 digits after the decimal point
Show trailing zeros
Table 2: Use the following formatting:
Use the table heading shown in the sample output
Right justification
Fixed format
5 digits after the decimal point
Show trailing zeros
Table 3: Use the following formatting:
Use the table heading shown in the sample output
Left justification
Scientific format
2 digits after the decimal point
Show trailing zeros
Enter the radius for circle #1 (in cm): 2
Enter the radius for circle #2 (in cm): 5
Table #1:
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.000 4.000 12.566 12.566
5.000 10.000 31.416 78.540
Table #2:
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.00000 4.00000 12.56637 12.56637
5.00000 10.00000 31.41593 78.53982
Table #3:
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.00e+000 4.00e+000 1.26e+001 1.26e+001
5.00e+000 1.00e+001 3.14e+001 7.85e+001
Enter the radius for circle #1 (in cm): 2
Enter the radius for circle #2 (in cm): 5
Table #1:
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.000 4.000 12.566 12.566
5.000 10.000 31.416 78.540
Table #2:
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.00000 4.00000 12.56637 12.56637
5.00000 10.00000 31.41593 78.53982
Table #3:
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.00e+000 4.00e+000 1.26e+001 1.26e+001
5.00e+000 1.00e+001 3.14e+001 7.85e+001
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double r1, r2;
cout<<"Enter the radius for circle #1 (in cm): ";
cin >> r1;
cout<<"Enter the radius for circle #2 (in cm): ";
cin >> r2;
cout<<"Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)"<<endl;
cout<<left<<fixed<<setprecision(3)<<r1<<" "<<2*r1<<" "<<2* M_PI * r1<<" "<<M_PI * r1 * r1<<endl;
cout<<left<<fixed<<setprecision(3)<<r2<<" "<<2*r2<<" "<<2* M_PI * r2<<" "<<M_PI * r2 * r2<<endl;
cout<<endl;
cout<<"Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)"<<endl;
cout<<right<<fixed<<setprecision(5)<<r1<<" "<<2*r1<<" "<<2* M_PI * r1<<" "<<M_PI * r1 * r1<<endl;
cout<<right<<fixed<<setprecision(5)<<r2<<" "<<2*r2<<" "<<2* M_PI * r2<<" "<<M_PI * r2 * r2<<endl;
cout<<endl;
cout<<"Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)"<<endl;
cout<<left<<scientific <<setprecision(2)<<r1<<" "<<2*r1<<" "<<2* M_PI * r1<<" "<<M_PI * r1 * r1<<endl;
cout<<left<<scientific <<setprecision(2)<<r2<<" "<<2*r2<<" "<<2* M_PI * r2<<" "<<M_PI * r2 * r2<<endl;
return 0;
}
Output:
sh-4.2$ main
Enter the radius for circle #1 (in cm): 2
Enter the radius for circle #2 (in cm): 5
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.000 4.000 12.566 12.566
5.000 10.000 31.416 78.540
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.00000 4.00000 12.56637 12.56637
5.00000 10.00000 31.41593 78.53982
Radius(cm) Diameter(cm) Circumference(cm) Area(cm^2)
2.00e+00 4.00e+00 1.26e+01 1.26e+01
5.00e+00 1.00e+01 3.14e+01 7.85e+01