Moon Surface Area Program

Write a C++ program that allows the user to enter the % of the moon's face that appears illuminated, and that outputs the surface area of that portion of the moon.
The moon's radius is 1738.3 km so for 100% of the moon illuminated (full moon), is would be:
S = 2 x 1738.3 x 3.14159 = 18985818.672 square kilometres.

I created a program but I can't get it to go to the exact decimal (shows 18985820.000), and I can only get the 100% calculation to work. Help!?



#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

const float PI = 3.14159265;
const float radius = 1738.3;
float area;
float rate;


cout << "Please enter the % of the moon's face that appears illuminated: ";
cin >> rate;
cout << "The surface area based on your percentage is: ";
cout << fixed << setprecision(3) << 2 * pow(radius,2) * PI * (rate/100) << " square kilometres." << endl;

return 0;
}
sorry i change it around a little bit but all you need to do is change the float to a double and it works.

when using constants make them all caps

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main( )
{

    const double PI = 3.14159265;
    const double RADIUS = 1738.3;

    double area;
    double rate;

    cout << fixed << setprecision( 3 );

    cout << "Please enter the % of the moon's face that appears illuminated: ";
    cin >> rate;

    cout << endl;

    area = 2 * pow( RADIUS, 2 ) * PI * ( rate / 100 );

    cout << "The surface area based on your percentage is: ";
    cout << area << " square kilometres." << endl;

    return( 0 );
}


Last edited on
Thank you so much!!!! I guess I was on the right track..just missing the right information.
Topic archived. No new replies allowed.