the loop is not getting stop in this java program..

public class Squarerootguess {
   
    public  static void main(String[] args){
       
        squareRoot(12.0);
       
    }
   
    public static double squareRoot(double m){
          double a;
        a = 144;
       
      // m = 12.0;
        double guess;
        guess = 72.0;
       
        while (guess >= m){
            System.out.println(guess);

              if (guess >= m) {
                     double x ;
                        x = (guess + (a/guess))/2;
                       // System.out.println(x);
                        guess = x;
              }else  {
                System.out.println("Mission LOOP accomplised");
  }
      }
            return m;
    }
    }
shravan telaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Without your code being in code tags, I’ll need to take a wild guess at the reason for this, and my guess would be that ‘guess’ is always >= m.
0
Bill BachPresident and Btrieve GuruCommented:
Agreed.  Given that these are floating point values, even if the code is working to approximate the square root, it will likely get to 12.000000000000000000001 or even smaller.  Might want to add a rounding function in there somewhere to exit the loop when it is "close enough".
0
krakatoaCommented:
Don't you need something more like this, shravan tela ? :

public class Squarerootguess {
    
    public  static void main(String[] args){
        
        //squareRoot(12.0);//org
        System.out.println("The square root is : "+squareRoot(144.0));
        
        
    }
    
    public static double squareRoot(double m){
    double previousguess=0.0;
          //double a;//org
        //a = 144;//org
        
      // m = 12.0;
        double guess;
        //guess = 72.0;//org
        guess = m/2;
        
        //while (guess >= m){//org
        while (guess*guess>m){
            //System.out.println(guess);//org

              //if (guess >= m) {//org
              if (m>guess) {
                     //double x ;//org
                        //x = (guess + (a/guess))/2;//org
                        
                        guess = (guess + (m/guess))/2;
                        if((guess==previousguess)){break;}
                        previousguess=guess;
                        try{
                        System.out.println(guess);
                        Thread.sleep(10);
                        }catch(Exception ex){}
                       // System.out.println(x);
                        //guess = x;//org
              }/*else  {
                //System.out.println("Mission LOOP accomplised");//org
  }*/
                
      }
            System.out.println("Mission LOOP accomplised");
            //return m;//org
            return guess;
            
    }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
Handled
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.