Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- static Scanner input = new Scanner(System.in);
- public static void Babylonian (){
- System.out.println("Take a guess");
- double n = input.nextDouble();
- double r = n / 2;
- //System.out.println("The initial guess is " + r);
- double last_guess = r;
- double current_guess = r;
- double one_percent = 1;
- while (true) {
- last_guess = current_guess;
- r = n / current_guess;
- current_guess = (current_guess + r) / 2;
- System.out.println("The current guess is " + String.format("%.2f", current_guess) );
- System.out.println("The last guess is " + String.format("%.2f", last_guess) );
- System.out.println("The r is " + String.format("%.2f", r) );
- one_percent = ((current_guess - last_guess) / last_guess) * 100;
- System.out.println("1% = " + String.format("%.2f", one_percent) );
- if (one_percent <= 1 && one_percent >= 0) {
- break;
- }
- }
- }
- public static void main(String[] args) {
- // write your code here
- Babylonian();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement