Wap to find sqrt of a number without sqrt function in Java
Answers
Answered by
0
import java.util.Scanner;
public class FindSquareRootExample1
{
public static void main(String[] args)
{
System.out.print("Enter a number: ");
//creating object of the Scanner class
Scanner sc = new Scanner(System.in);
//reading a number form the user
int n = sc.nextInt();
//calling the method and prints the result
System.out.println("The square root of "+ n+ " is: "+squareRoot(n));
}
//user-defined method that contains the logic to find the square root
public static double squareRoot(int num)
{
//temporary variable
double t;
double sqrtroot=num/2;
do
{
t=sqrtroot;
sqrtroot=(t+(num/t))/2;
}
while((t-sqrtroot)!= 0);
return sqrtroot;
}
}
public class FindSquareRootExample1
{
public static void main(String[] args)
{
System.out.print("Enter a number: ");
//creating object of the Scanner class
Scanner sc = new Scanner(System.in);
//reading a number form the user
int n = sc.nextInt();
//calling the method and prints the result
System.out.println("The square root of "+ n+ " is: "+squareRoot(n));
}
//user-defined method that contains the logic to find the square root
public static double squareRoot(int num)
{
//temporary variable
double t;
double sqrtroot=num/2;
do
{
t=sqrtroot;
sqrtroot=(t+(num/t))/2;
}
while((t-sqrtroot)!= 0);
return sqrtroot;
}
}
Answered by
1
Wap to find sqrt of a number without sqrt function in Java.
______________________________
class squreroot
{
void main(int n)
{
double squ=Math.pow(n1/2.0);
System.out.println(squ);
}
}
____________________________
Input:-9
Output:- 3.0
Similar questions