Write a JAVA program to find the square root of a number.
Answers
Answered by
2
/*Java program to find out square root of a given number * without using any Built-In Functions */public class SquareRootDemo2{ public static void main(String[] args) { //Number for which square root is to be found double number = -12; //This method finds out the square root findSquareRoot(number); } /*This method finds out the square root without using any built-in functions and displays it */ public static void findSquareRoot(double number) { boolean isPositiveNumber = true; double g1; //if the number given is a 0 if(number==0) { System.out.println("Square root of "+number+" = "+0); } //If the number given is a -ve number else if(number<0) { number=-number; isPositiveNumber = false; } //Proceeding to find out square root of the number double squareRoot = number/2; do { g1=squareRoot; squareRoot = (g1 + (number/g1))/2; } while((g1-squareRoot)!=0); //Displays square root in the case of a positive number if(isPositiveNumber) { System.out.println("Square roots of "+number+" are "); System.out.println("+"+squareRoot); System.out.println("-"+squareRoot); } //Displays square root in the case of a -ve number else { System.out.println("Square roots of -"+number+" are "); System.out.println("+"+squareRoot+" i"); System.out.println("-"+squareRoot+" i"); } }}
VishalAnand:
Have you copie it form the internet? Can you explain me!!!
Answered by
12
Answer:
import java.util.Scanner;
public class Roots
{
void compute( )
{
Scanner sc=new Scanner(System.in) ;
int num;
System.out.print("Enter integer number: ");
num=sc.nextInt( );
System.out.println("Square Root of" + num + "is:" + Math.sqrt(num)) ;
}
}
Explanation:
The Math.sqrt methods returns the square root of a number given as its arguments.
Similar questions
Biology,
7 months ago
Social Sciences,
1 year ago
English,
1 year ago
Math,
1 year ago
Hindi,
1 year ago