Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sqrt Program, skips over method

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Sqrt Program, skips over method

    Hey guys , I have to do a program where you must find the square root of a number by repeatedly divide a number by the number and past guess untill the diffrence is withing .000001. My code doesnt output , and when it did it would give me 0. If anyoene can help me out itd be much appricated. thanks!


    import java.util.*;
    import java.lang.Math;
    public class sqrt
    {
    public static void main(String[] args)
    {
    double value;
    double guess;
    Scanner in= new Scanner(System.in);
    System.out.println("Please enter the number of which you want to squareroot: ");
    value=in.nextDouble();
    System.out.println("Please enter youre first estimate: ");
    guess=in.nextDouble();
    while(guess<(value*.5))
    {
    System.out.print("Please input a higher value: ");
    guess=in.nextDouble();
    }
    System.out.println("The squareroot is: "+ sqrt(value,guess));

    }

    private static double sqrt(double value, double guess)
    {
    double nextGuess=guess*2;
    while( ((nextGuess-guess)!=.00001)||((nextGuess-guess)!=-.00001) )
    {
    nextGuess=(guess+(value/guess))/2;
    guess=nextGuess;
    }
    return guess;

    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sqrt Program, skips over method

    Please post your code in code tags.

    Your problem statement and description of the problem are not helpful. Here's my sample run:
    Please enter the number of which you want to squareroot: 
    51
    Please enter youre first estimate: 
    7
    Please input a higher value: 8
    Please input a higher value: 8
    Please input a higher value: 8
    Please input a higher value: 8
    Please input a higher value:
    Seems to be running, even outputting, though it's annoying and I have no idea what it's trying to do. Why don't you post a sample run and describe what it should be doing.

    Oh, and there's no "skips over method" that I can see.

Similar Threads

  1. [SOLVED] My loop skips index[0] of my array
    By Benner in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2013, 06:09 PM
  2. [SOLVED] BufferedReader skips odd numbered lines
    By beer-in-box in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2013, 07:46 AM
  3. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  4. Math.sqrt causing general havoc
    By Garrett93 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2012, 03:43 AM
  5. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM