Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class var_27_task_3 {
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- boolean isIncorrect;
- int precision = 0;
- do {
- System.out.print("Enter precision (number of iterations) : ");
- isIncorrect = false;
- try {
- precision = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException exception) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- if (!isIncorrect && (precision <= 0)) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- } while (isIncorrect);
- scanner.close();
- double pi = 0.0;
- float numerator = 1;
- float firstDenominator = 1;
- float secondDenominator = 3;
- for (int i = 0; i < precision; i++) {
- for(int k = 0; k < 3; k++){
- if (k < 2){
- pi += numerator/(firstDenominator*secondDenominator);
- } else {
- pi -= numerator/(firstDenominator*secondDenominator);
- }
- firstDenominator+=4;
- secondDenominator+=4;
- }
- }
- System.out.println("Pi value = " + pi*8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement