Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     static Scanner input = new Scanner(System.in);
  8.  
  9.     public static void Babylonian (){
  10.         System.out.println("Take a guess");
  11.         double n = input.nextDouble();
  12.  
  13.         double r = n / 2;
  14.         //System.out.println("The initial guess is " + r);
  15.  
  16.         double last_guess = r;
  17.         double current_guess = r;
  18.  
  19.         double one_percent = 1;
  20.  
  21.         while (true) {
  22.             last_guess = current_guess;
  23.             r = n / current_guess;
  24.             current_guess = (current_guess + r) / 2;
  25.  
  26.             System.out.println("The current guess is " + String.format("%.2f", current_guess) );
  27.             System.out.println("The last guess is " + String.format("%.2f", last_guess) );
  28.             System.out.println("The r is " + String.format("%.2f", r) );
  29.  
  30.  
  31.             one_percent = ((current_guess - last_guess) / last_guess) * 100;
  32.             System.out.println("1% = " + String.format("%.2f", one_percent) );
  33.             if (one_percent <= 1 && one_percent >= 0) {
  34.                 break;
  35.             }
  36.         }
  37.     }
  38.  
  39.     public static void main(String[] args) {
  40.     // write your code here
  41.         Babylonian();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement