0

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.

Input: Read the variable R (double precision), that is the radius of the circle.
Output: Print the variable A, rounded to four decimal digits.

Sample Input : 2  
Sample Output: A=12.5664

i have tried this ,

import java.util.Scanner;


 public class Area { 
    public static void main(String a[]) 
    {       
      Double R,A;
      Scanner in=new Scanner(System.in);
      R=in.nextDouble();
      A=3.14159*R*R;
      System.out.println("A="+(Math.round(A*1000.0)/10000.0));
     }

 }
Aditya Jain
  • 1,077
  • 1
  • 12
  • 25
S-N
  • 1
  • 1
  • 1
    Use `BigDecimal` and its rounding methods: http://stackoverflow.com/questions/15352229/rounding-mode-with-bigdecimal-in-java – Boris Pavlović Jun 03 '14 at 12:12
  • Refer http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Ankit Zalani Jun 03 '14 at 12:14
  • btw. there is a constant `Math.PI` – oliholz Jun 03 '14 at 12:18
  • You cannot round a double in memory. Just round it on display. If you want to round it automatically to the significant figure count, you will have to store the significant figure count in a separate int. – bokan Jun 03 '14 at 12:20

2 Answers2

0

SetRoundingMode will do the job.

or

You can also use DecimalFormat class:

double A=3.14159;

DecimalFormat no= new DecimalFormat("#.####");
double no=  Double.valueOf(newFormat.format(A));
Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
0

Try this:

import java.text.DecimalFormat;
import java.util.Scanner;

    public class Area {
    public static void main(String a[]) {
        Double R, A;
        Scanner in = new Scanner(System.in);
        R = in.nextDouble();
        A = 3.14159 * R * R;
        DecimalFormat d = new DecimalFormat();
        d.setMinimumFractionDigits(4);
        d.setMaximumFractionDigits(4);

        System.out.println("A=" + d.format(A));
    }

}
Sanjeev
  • 9,876
  • 2
  • 22
  • 33