Square root program in java

import javax.swing.*;
public class sqrt
{
public static void main(String args[])
{
String tempString=JOptionPane.showInputDialog(“Enter a positive number”);
double number=Double.parseDouble(tempString);
System.out.println(“Square root of ” + number+”=”+sqrt(number));
}
public static double sqrt(double number)
{ //square root Babylonian method

double estimate=number;
double divisor=2;
//below 100 is arbitrary, for very small decimals  i values must be large
for(int i=0; i<100; i++)
{
estimate=number/divisor;
estimate=(estimate+divisor)/2; //find average estimate & divisor
divisor=estimate;
}
return estimate;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s