Welcome to Dream.In.Code
Become a Java Expert!

Join 149,585 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,784 people online right now. Registration is fast and FREE... Join Now!




Help using Square Root Algorithm

 
Reply to this topicStart new topic

Help using Square Root Algorithm

shay_hookshot_07
5 Oct, 2007 - 11:19 AM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2007
Posts: 2


My Contributions
My assignment is to "guess" the square root using Newton's method.
I cannot figure out why it will not work?
I think it is something in the Why expression...
Java does not print out an "error"..it simply give an incorrect answer.
For example...it says the square root of 9 is 3.250000


import java.util.Scanner;
public class CalcSqrt
{
public static void main (String [] args)
{
Scanner sc= new Scanner (System.in);
System.out.print ("Enter positive number: ");

double N= sc.nextInt ();
double oldGuess=N/2.0;
double newGuess= ( (N / oldGuess) + oldGuess) / 2.0;

while ( (Math.abs(oldGuess - newGuess) <= .000001) && (newGuess > oldGuess) );
{
oldGuess=newGuess;
newGuess= ( (N / oldGuess) + oldGuess) / 2.0;
++oldGuess;
}


System.out.printf ("You entered " +N+ ", the square root is %.6f", newGuess);
}
}

This post has been edited by shay_hookshot_07: 5 Oct, 2007 - 12:33 PM
User is offlineProfile CardPM
+Quote Post

rickyadd
RE: Help Using Square Root Algorithm
5 Oct, 2007 - 12:01 PM
Post #2

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 8


My Contributions
put the error message on here

This post has been edited by rickyadd: 5 Oct, 2007 - 12:03 PM
User is offlineProfile CardPM
+Quote Post

shay_hookshot_07
RE: Help Using Square Root Algorithm
5 Oct, 2007 - 12:34 PM
Post #3

New D.I.C Head
*

Joined: 5 Oct, 2007
Posts: 2


My Contributions
QUOTE(rickyadd @ 5 Oct, 2007 - 01:01 PM) *

put the error message on here


Sorry..sad.gif

it does not give an error
it simply computes the incorrect answer

for example : it says the square root of 9 is 3.250000

(the loop is ending when it shouldn't)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month