# Square Root Java Program

Page 1 of 1

## 10 Replies - 24327 Views - Last Post: 01 January 2012 - 09:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=261508&amp;s=c91747f8d4e7d67dc49b2e2d4b51aba4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pcpeterchen

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 22-December 11

# Square Root Java Program

Posted 30 December 2011 - 09:06 PM

Hello all,
I'm having trouble with my while loop in the sqrt method. Any ideas on how to make it work?

```//Program requirements: user inputs a number and the program outputs a square root.
/*Program design:  Calculators work by using the Newton's Method or Babylonian method by tral and error.
You begin with variable root x and a starting x0.
Then the average of the over estimate and the underestimate will eual an approximation close to the square root.
If you repeat the proccess with the new approximation you get an even closer value.
If the process is repeated, eventually you will get within a certain accuracy.*/
// Calculators only have 8 decimall places, so I'll go to 8 points of accuracy.
//^^/> this is my own thinking, not real instructions.
import java.util.Scanner;
public class SquareRoot
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
Sqrt ans = new Sqrt();
double num = scan.nextDouble();
System.out.println(ans.sqrt(num));
}
}

```

```import java.util.Scanner;
import java.lang.Math;
public class Sqrt
{
public double sqrt(double num)
{
double x = 1;
double y=1;
while(Math.abs(x-y) < 0.00001)
{
y=x;
x = 0.5*(x + num/ x);
}
return x;
}
}import java.util.Scanner;
import java.lang.Math;
public class Sqrt
{
public double sqrt(double num)
{
double x = 1;
double y=1;
while(Math.abs(x-y) < 0.00001)
{
y=x;
x = 0.5*(x + num/ x);
}
return x;
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Square Root Java Program

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Square Root Java Program

Posted 30 December 2011 - 09:16 PM

You will have to input from the user before building your Sqrt object that actually expects a double as parameter

and NOT a good idea at all to have a method named sqrt in a class named Sqrt
it is very confusing

*Edited: thus my error

This post has been edited by pbl: 30 December 2011 - 09:26 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Square Root Java Program

Posted 30 December 2011 - 09:31 PM

```public class SquareRoot
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
double num = scan.nextDouble();
Sqrt ans = new Sqrt(num);
System.out.println(ans.calculate());
}
}

public class Sqrt
{
double num;
public Sqrt(double num)
{
this.num = num;
}

double calculate() }
double x = 1;
double y=1;
while(Math.abs(x-y) < 0.00001)
{
y=x;
x = 0.5*(x + num/ x);
}
return x;
}
}
```

### #4 pcpeterchen

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 22-December 11

## Re: Square Root Java Program

Posted 31 December 2011 - 11:49 AM

thanks a bunch!!! ^____^

### #5 pcpeterchen

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 22-December 11

## Re: Square Root Java Program

Posted 31 December 2011 - 11:55 AM

but... it's still not outputting the correct answer!!!

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Square Root Java Program

Posted 31 December 2011 - 01:48 PM

Please post your updated code and be specific about how the program's results are incorrect or do not meet your expectations. I'm sure it's a simple fix, but without your code we'd be guessing at it.

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,396
• Joined: 27-December 08

## Re: Square Root Java Program

Posted 31 December 2011 - 02:42 PM

You only want to multiply the second term by 0.5, not the entire expression. Remember that:
x[i+1] = x[i] + f(x[i])/f'(x[i]).

I have a tutorial on the Newton method for more information.

### #8 pcpeterchen

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 22-December 11

## Re: Square Root Java Program

Posted 01 January 2012 - 08:41 PM

by the way... how do I delete an account on dream.in.code?

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,396
• Joined: 27-December 08

## Re: Square Root Java Program

Posted 01 January 2012 - 08:47 PM

### #10 pcpeterchen

• New D.I.C Head

Reputation: 0
• Posts: 11
• Joined: 22-December 11

## Re: Square Root Java Program

Posted 01 January 2012 - 09:03 PM

k thanks!

### #11 Crockeo

Reputation: 44
• Posts: 247
• Joined: 21-June 11

## Re: Square Root Java Program

Posted 01 January 2012 - 09:46 PM

I'd hate to go off topic, but am I the only one who finds it amusing that someone is asking on Dream In Code how to delete their account right after having their question asked?