Java Program To Calculate Square Root Of A Number

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.

Active5 years, 9 months ago

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

user3050340user3050340
1741 gold badge3 silver badges11 bronze badges

3 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:

Michael YaworskiMichael Yaworski
9,94516 gold badges51 silver badges82 bronze badges

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.

joewsjoews
21.9k10 gold badges59 silver badges75 bronze badges

change:

for:

you're calling a method, 'print' does not make any sense there.

lcjurylcjuryJava Program To Calculate Square Root Of A Number
6321 gold badge6 silver badges20 bronze badges
Java

Not 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

  1. Define value n to find the square root of.
  2. Define variable i and set it to 1. (For integer part)
  3. Define variable p and set it to 0.00001. (For fraction part)
  4. While i*i is less than n, increment i.
  5. Step 4 should produce the integer part so far.
  6. While i*i is less than n, add p to i.
  7. Now i have the square root value of n.

Program

Calculate Square


Output