If we want to calculate square root, we can use Math.sqrt() method. It is simple. Now do you know how to write such a method by yourself? Here is the equation you need. The first sqrt number should be the input number / 2. Using the equation, we can come up with a Java Square Root method by ourselves. 1) Program 1: Prompting user for entering the side of the square 2) Program 2: Side of the square is specified in the program’ s source code. Program 1: /** * @author: BeginnersBook.com * @description: Program to Calculate Area of square.Program * will prompt user for entering the side of the square. In Java write a program to compute the square root of a number. DO NOT USE any math libraries/methods in this program. You will be using the Babylonian method (a.k.a. Heron’s method) to approximate the square root.
I am trying to write a code to return a square of a number
I think my method is basically complete but then I have trouble with compiling,
here is my code
user3050340user30503403 Answers
Change print square(num);
to System.out.println(square(num));
or to square(num);
Although, what you're doing makes no sense because you actually print the number in your method as well. Try changing your code to this:
There isn't a print
statement in Java - you need to use the method System.out.println(square(num));
Program To Calculate Square Footage
Although, actually, your square
method is writing the squared number to STDOUT
anyway, so you don't need to do any more printing - just square(num);
would be enough.
change:
for:
you're calling a method, 'print' does not make any sense there.
lcjurylcjuryNot the answer you're looking for? Browse other questions tagged java or ask your own question.
- Related Questions & Answers
Java Program To Calculate Square Root Of A Numbers
- Selected Reading
The process of finding the square root of a number can be divided into two steps. One step is to find integer part and the second one is for fraction part.
Algorithm
- Define value n to find the square root of.
- Define variable i and set it to 1. (For integer part)
- Define variable p and set it to 0.00001. (For fraction part)
- While i*i is less than n, increment i.
- Step 4 should produce the integer part so far.
- While i*i is less than n, add p to i.
- Now i have the square root value of n.