import java.io.*; class squareRoot { public static void main(String args[])throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number"); int n = Integer.parseInt(br.readLine()); int sq = 0; for(int i=1; i<(n/2); i++) { if((i*i) == n) { sq = i; break; } } if(sq == 0) System.out.println("Not a perfect square"); else System.out.println("Square root = " + sq); } }

But this would calculate the square root of a perfect square like 25 or 36 only. So how should I write some code which can calculate the square root of any number?

Maybe I could use double type variables for i (loop variable) and sq, and increase i by something like 0.001. But this would make the program highly ineffiecient and time-consuming and still incompetent for some numbers.

This post has been edited by **adhish94**: 10 August 2010 - 03:17 AM