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
You will probably have to make use of some approximation method.
Have a look at
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.
There are numerous methods to compute Square root. Try this http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
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?