Area and Volume of a cone with step increments of 0.5

Hello, I am new to C++ so try to not be too hard on me. I have been searching the forums and google for a few hours to try and figure this out. I am trying to calculate the area and volume of a cone from 10-20 with a step increment of 0.5 with a while loop and I cannot figure out why my calculations are correct, but the numbers are not matching up. I have tried setting r and h to r=r=.5; and h the same, but it will not compute. There are helpful articles for the for loop, but not while.

It is setting the correct calculations for 10 on 11's column and I have tried setting int r=9, but the calculations are not matching up.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using namespace std;

 

/*

 *

 */

int main(int argc, char** argv)

{

const double PI =3.14159265359; 

int h=10; //height

int r=10; //radius

double area=0.0;

double volume=0.0;

cout<<"The program calculates the area and volume of a cone from 10-20"<<endl;
cout<<"in 0.5 increments."<<endl;
cout<<" "<<endl;

cout<<" Radius      Area       Volume"<<endl;

cout<<fixed<<setprecision(0);

    //while loop
     while (r<=20)
 
        {
           
            //area of a cone
        area= PI*r*(r+sqrt(h*h+r*r));
           
            //volume of a cone 
        volume = PI*r*r*(h/3.0);

         r++;
         h++;

cout<<setw(4)<<r<<setw(14)<<setprecision(2)<<area<<setw(12)<<volume<<endl;

}

return 0;

}


Output:
The program calculates the area and volume of a cone from 10-20
in 0.5 increments.

Radius Area Volume
11 758.45 1047.20
12 917.72 1393.82
13 1092.16 1809.56
14 1281.78 2300.69
15 1486.56 2873.51
16 1706.51 3534.29
17 1941.63 4289.32
18 2191.91 5144.88
19 2457.37 6107.26
20 2738.00 7182.73
21 3033.79 8377.58

RUN SUCCESSFUL (total time: 91ms)

Last edited on
h and r need to be floating point (double) if you want to increment them by 0.5, and the increment should occur after you print a line of the table. So maybe something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main() {
    const double PI = 3.14159265359; 
    double h = 10.0; //height
    double r = 10.0; //radius
    double area = 0.0;
    double volume = 0.0;

    cout << "The program calculates the area and volume of a cone";
    cout << "from 10-20 in 0.5 increments.\n\n";
    cout << "Radius      Area       Volume\n";
    cout << fixed;

    while (r <= 20) {
        //area of a cone
        area= PI * r * (r + sqrt(h * h + r * r));
        //volume of a cone 
        volume = PI * r * r * h / 3.0;
        cout << setprecision(1) << setw( 4) << r      << ' '
             << setprecision(2) << setw(14) << area   << ' '
                                << setw(12) << volume << '\n';
        r += 0.5;
        h += 0.5;
    }
}

Last edited on
Thank you so much for the fast reply. The teacher gave us some sample code to use on the assignment and it was not helpful at all on increasing increments in a while loop and online most of what i could find was for for loops. Appreciate it so much.

The program calculates the area and volume of a cone from 10-20
in 0.5 increments.

Radius Area Volume
10 758.45 1047.20
10.50 836.19 1212.26
11.00 917.72 1393.82
11.50 1003.05 1592.66
12.00 1092.16 1809.56
12.50 1185.07 2045.31
13.00 1281.78 2300.69
13.50 1382.27 2576.50
14.00 1486.56 2873.51
14.50 1594.64 3192.51
15.00 1706.51 3534.29
15.50 1822.17 3899.63
16.00 1941.63 4289.32
16.50 2064.87 4704.14
17.00 2191.91 5144.88
17.50 2322.75 5612.32
18.00 2457.37 6107.26
18.50 2595.79 6630.46
19.00 2738.00 7182.73
19.50 2884.00 7764.84
20.00 3033.79 8377.58

RUN SUCCESSFUL (total time: 167ms)

It works perfectly!
Topic archived. No new replies allowed.