write a program to find square and cube root of a number
Answers
Explanation:
1.Square Root:-
There are many methods to find the square root of a
number. The algorithm that you will use depends on
what is needed more- accuracy or speed. (Although
speed is trivial thing for today's processors.)
Babylonian Method:
This method predates every method except the vedic
duplex method.The idea behind this method is that
if a number x is close to the required square root of n,
then iterative approximation of the average of x and
n/x can be used to converge to the root.
Below is the algorithm and its implementation in C.
sQUARE_ROOT (n)
2x = n;
3 y= 1;
4
while(x-y > a) /lr approximation
5 x (x+y)/2
6 y= n/x;
return x;
Answer:
import java.util.Scanner;
public class powerRoots
{
void compute( )
{
Scanner sc=new Scanner(System.in) ;
int num;
System.out.print("Enter integer number: ");
num=sc.nextInt( );
System.out.println("Square of" + num + "is:" + Math.pow(num1,2)) ;
System.out.println("Cube Root of" + num + "is:" + Math.cbrt(num));
}
}
Explanation:
The Math.pow method returns first argument (called the base number) raised to power of second-argument (called the exponent).That is, return value of Math.pow( X,Y ) is X^y
The Math.cbrt methods returns the cube root of a number given as its arguments.