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.

Results 1 to 6 of 6

Thread: Java program Square root

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Java program Square root

    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).


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Java program Square root

    Math.sqrt(n);

    Easy I'm sure you can work it out

    Regards,
    Chris

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java program Square root

    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.

  4. #4
    Junior Member
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java program Square root

    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;
           }
    }
    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.

  5. The Following User Says Thank You to hajidel For This Useful Post:

    JavaPF (August 15th, 2009)

  6. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: Java program Square root

    Quote Originally Posted by hajidel View Post
    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.
    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);
    }}

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Java program Square root

    You can use
    3.1622776601683793319988935444327*log(x)
    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.

    Chris

Tags for this Thread