you need to know that
Number of digits after the decimal point
needs to use double, because
Wrong Answer in simple problem using floating-point
Try to use double instaed float in C++. This is a common error due the precision of each one of them.
here is the uri online judge 1002 problem: http://www.urionlinejudge.com.br/judge/en/problems/view/1002
Area of a Circle
The area of the circle is defined as A = π . R2, having π as 3.14159.
Calculate the area using the formula given in the problem description.
Calculate the area using the formula given in the problem description.
Input
Read the variable R, that is the radius of the circle.
Output
Print the variable A, rounded to four decimal digits.
Sample Input | Sample Output |
2 | A=12.5664 |
and here is my solution in c++: http://ideone.com/lQPoPh
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main() {
- double A, p = 3.14159, R;
- cin >> R;
- A = p * R * R;
- cout << fixed << setprecision(4);
- cout << "A=" << A << endl;
- return 0;
- }
and here is another solution in c++: http://ideone.com/hiebng
#include <iostream>
#define pi 3.14159
using namespace std;
long double r,area;
int main(){
cin>>r; area=(r*r)*pi;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(4);
cout <<"A=" << area;
return 0;
}
(the above shown solution belongs to Quan Tran.)
No comments:
Post a Comment