views:

300

answers:

5

how can i create method which return sqrt of given nunber? for example sqrt(16) returns 4 and sqrt(5) returns 2.3... please help i am using java and know Math.sqrt() API function but i need method itself

+6  A: 

You will probably have to make use of some approximation method.

Have a look at

Methods of computing square roots

astander
+1 for the link
Iulian Şerbănoiu
A: 

Have you tried wikipedia

Daniel Băluţă
+2  A: 

Here's something to think about:

To find a square root, you simply need to find a number which, raised to the power of 2 (although just multiplying by itself is a lot easier programmatically ;) ) gives back the input.

So, start with a guess. If the product is too small, guess larger. If the new product is too large, you've narrowed it down - guess somewhere in between. You see where I'm going...

Depending on your need of precision and/or performance, there are of course lots of ways. The solution hinted at in this post is in no way the best one in either of those categories, but it gives you a clue on one way to go.

Tomas Lycken
That will take lot of processing though!
JavaGeek
+6  A: 

There are numerous methods to compute Square root. Try this http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

1s2a3n4j5e6e7v
A: 

If I've interpreted the question right... You are a new student somewhere.

You shouldn't be asking so simple things here. And it sounds more like a program than method.

Here's how you can get on with such program:

  • Find some chassis of java program.
  • Locate standard input read and standard output write -actions.
  • insert write(sqrt(string_to_integer(read()))) into the main program block.

I don't bother with java so I don't know the particular details. http://www.devdaily.com/java/edu/pj/pj010005/

UGH. I just remembered why I don't bother with java at all. :D Who raped its IO library?

Cheery