-2

I need to calculate the square root of an entered number by a manual calculation with the constant EPSILON as long as (x2 - a) > EPSILON. What is wrong with my code?

import java.util.Scanner;
import java.lang.Math;
public class ProgrammeRacineCarree
{
    public static Scanner clavier = new Scanner(System.in);

    public static void main(String[] args){
        System.out.print("\f");
        double a = clavier.nextInt();
        double x = a;
        double xadeux = java.lang.Math.pow(x,2);
        double epsilon = 0.0001;

        while((xadeux-a)>epsilon){
            x = (x/2)+(a/(2*x));
        }

        System.out.println("La valeur de la racine carrée de " + a + " est de " + x + " .");

    }
}
Matthew
  • 1,412
  • 2
  • 20
  • 35
  • 4
    Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – Turing85 Sep 08 '19 at 19:40

1 Answers1

3

your loop is iterating on variables: xadeux, a and epsilon

the parameter your change inside the loop is x

that is why the loop is infinite because (xadeux-a)>epsilon) stays always the same

Kukula Mula
  • 1,788
  • 4
  • 19
  • 38