Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK SPEAKING MVC ASP.NET JQUERY VIDEOS EBOOK ARCHIVE
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

23 May 2012

Find square root in java without using any system function


import java.util.Scanner;
public class Sqrt {
 
public void getTheSqrt( int n )
{
// double a = (eventually the main method will plug values into a)
double a = (double) n;
double x = 1;
 
// For loop to get the square root value of the entered number.
for( int i = 0; i < n; i++)
{
x = 0.5 * ( x+a / x );
}
 
System.out.println("sqrt of " + a + " is " + x);
}
 
// Begin method main..
public static void main(String[] args) {
// Creating an object from "Scanner" to input data.
Scanner Sa = new Scanner(System.in);
Sqrt S = new Sqrt();
 
//Asking the user to input the number we want to find its square root.
System.out.println("Enter n: ");
int n = Sa.nextInt();
 
// Calling the method that'll calculate the square root of the given number.
S.getTheSqrt( n );
 
} // End method main
 
}