Hi can someone post a java program that finds the square root of a number and please explain what each line of code does. Also add a method (for example Public Static void).
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi can someone post a java program that finds the square root of a number and please explain what each line of code does. Also add a method (for example Public Static void).
Math.sqrt(n);
Easy I'm sure you can work it out
Regards,
Chris
Hey, Hey, lol Welcome to the Java Programming Forums.
Like Chris says, this is an easy one! Check the Math API
Math (Java 2 Platform SE 5.0)
import java.util.Scanner; public class Hey { /** * JavaProgrammingForums.com */ public static double number, answer; public static void calculateSquare(double number){ answer = Math.sqrt(number); System.out.println("The square root of " + number + " is " + answer); } public static void main(String[] args) { Hey hey = new Hey(); Scanner sc = new Scanner(System.in); System.out.println("Enter a double: "); number = sc.nextDouble(); hey.calculateSquare(number); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Looking for a Java job? Visit - Java Programming Careers
As they've already pointed out on the other threads, there are predefined classes to calculate square roots and most mathematical functions. But if you want to do it manually and know how it works, then this is one way.public class SquareRoot{ public static void main(String[] args){ Scanner sc = new Scanner(System.in()); //this creates an object of the scanner class to input from keyboard int someNumber; someNumber = sc.nextInt(); //reads an integer from the keyboard //calls the method to calculate the root and prints on screen// System.out.println("The square root is: " + sRoot(someNumber)); } private static double sRoot(int a){ int z = a; int root = (double) a * a; return root; } }
JavaPF (August 15th, 2009)
That code is going to only put out the square but not the square root... the following will do the root, had to do it for class earlier this year. Spent about four hours trying to figure out the convergent infinite series math and what not and then realized that trail and error by digit was a lot easier.
import java.util.*; public class SRoot { public static void main (String[] args) { Scanner k = new Scanner(System.in); System.out.println("Insert Number"); int number = k.nextInt(); int lowBound=34404; for (int i=0; i<100; i++) { if ( i*i <= number ) { lowBound = i; } } System.out.println(lowBound); double estimate = lowBound; double ten=0; for (double i=0; i<1; i+=.1) { if ((estimate+i)*(estimate+i) <=number) { ten = i; } } estimate = estimate + ten; double hund = 0; for (double i=0; i<.1; i+=.01) { if ((estimate+i)*(estimate+i) <=number) { hund = i; } } estimate = estimate + hund; double thou = 0; for (double i=0; i<.01; i+=.001) { if ((estimate+i)*(estimate+i) <=number) { thou = i; } } estimate = estimate + thou; System.out.println(estimate); }}
You can use
which is actually root 10 * log(x) to find it also. or read up on babylonian Method, the true iterative method, for those numbers whose square root is not an integer such as 8.3.1622776601683793319988935444327*log(x)
Chris