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?
 
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
 
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 BachPresidentCommented:
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:
Handled
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.