m2skills

squareroot java

Apr 18th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. //program to find square root of a number without using square root function
  2. import java.util.Scanner;
  3.  
  4. public class squareRoot{
  5.    
  6.     public static void main(String arg[]){
  7.         boolean cont = true;
  8.         do{
  9.             Scanner sc = new Scanner(System.in);
  10.             //taking input from the user
  11.             System.out.print("Enter the Number whose square root needs to be found out : ");
  12.             double number = sc.nextDouble();
  13.            
  14.             // if the number is less than 0 then ask user to enter another number
  15.             while(number < 0){
  16.                 System.out.println("Please enter a positive number");
  17.                 System.out.print("Enter the Number whose square root needs to be found out : ");
  18.                 number = sc.nextDouble();
  19.             }
  20.            
  21.             // if the number is 0 or 1 then the answer is also is also 0 or 1
  22.             if(number == 0 || number == 1){
  23.                 System.out.print("The square root of the number " + number + " is " + number + "\n\n");
  24.                 continue;
  25.             }
  26.            
  27.             //else take initial guess from the user
  28.             System.out.print("Enter the Initial guess : ");
  29.             double sqroot = sc.nextInt();
  30.            
  31.             //taking mod of the guess so as to make it smaller than the square
  32.             sqroot = sqroot % number;
  33.            
  34.             findroot(number,sqroot);
  35.             System.out.print("Do you want to enter another number (1/0) : ");
  36.             int n = sc.nextInt();
  37.             if(n == 0){
  38.                 cont = false;
  39.             }
  40.         }while(cont);
  41.     }
  42.    
  43.     public static void findroot(double number, double sqroot){
  44.        
  45.         // setting answer to be accurate upto 6 decimal places
  46.         // change this to get more accuracy
  47.         double error = 0.000001;
  48.        
  49.         // calculating num
  50.         double num = number/sqroot;
  51.        
  52.         // till the difference is greater than error keep taking average
  53.         while(((num < sqroot) && (sqroot - num > error)) || ((num > sqroot)&&(num - sqroot > error))){
  54.            
  55.             // taking average
  56.             sqroot = (sqroot + num)/2;
  57.             num = number/sqroot;
  58.  
  59.             // if num and sqroot are equal then we directly print the answer
  60.             if(num == sqroot){
  61.                 break;
  62.             }
  63.         }
  64.         System.out.print("The square root of the number " + number + " is " + sqroot + "\n\n");
  65.     }
  66. }
Add Comment
Please, Sign In to add comment