Go to Top

Tuesday 11 October 2011

Libraries 2 : Square root function

This program uses Newton's formula for calculating the square root of  a number taken as input from the user.

Java Program :

import java.io.*;
class sqrt_function
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int i;
     double a,b=2,c=0;
     System.out.print("Enter a number : ");
     a=Double.parseDouble(br.readLine());
     for(i=1;i<=10;i++)
     {
        c=(a/b);
        b=(c+b)/2;
     }
     System.out.print("\nSquare Root of "+a+" = "+b);  
    }
}

1 comment:

ShareThis