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).
Printable View
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).
Code :
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)
Code :
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); } }
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.Code :
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; } }
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.
Code :
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.Quote:
3.1622776601683793319988935444327*log(x)
Chris