Monte Carlo Calculation of pi in Java

<<algorithm

 

pi

import java.util.Scanner;

public class Pi {
  public static void main(String[] args) {
    Scanner key=new Scanner(System.in);
    System.out.print("n = ");
    int n=key.nextInt();
    double x,y;
    int k=0;
    for(int i=1; i<=n; i++){
      x=Math.random();
      y=Math.random();
      if(x*x+y*y<=1) k++;
    }
    double p=4.*k/n;
    System.out.println(p);
  }  
}

 

 

 

loadposition Spis-mod_AJ_en}