Go to Top

Thursday, 11 August 2011

To make a square root function

Question 40 : Write a program in Java to make a function that calculates the square root of a number given by 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);     
    }
}

No comments:

Post a Comment

ShareThis