+ 4
How to write a program to find sqaure root of a number without using math library?
8 Réponses
+ 2
There are many methods of computing square roots.
https://en.m.wikipedia.org/wiki/Methods_of_computing_square_roots
+ 1
Oh shit. I forgot were in java. i styled this code in c++ syntax by mistake. just make the necessary adjustments to main ()
+ 1
import java.util.Scanner;
class Sqrt
{
public static double sqrt(float num);
{
double guess = 0;
do
{
guess += .000001;
}while(guess*guess < num );
return guess;
}
}
class SqrtTest
{
public static void main(String[] args)
{
System.out.println("Please enter a number >> ");
Scanner cin = new Scanner(System.in);
float num = cin.nextFloat();
double result = sqrt(num);
System.out.println("The square " +
" of " + num + " is " + result);
}
}
+ 1
It works for me and very accurate result ->
result=1;
for(int i=1; i<=10; i++)
{
result=0.5*(result+n/result);
}
0
i guess math.sqrt() is the only way to find it
0
make your own library
0
class math
{ int a,b;
void sqr(int x)
{a=x;
b=a*a;
System.out.println(b);
}
}
class sqrt
{ public static void main( String raha[])
{math ob=new math();
ob.sqr(9);
}}
0
double sqrt(float num)
{
double guess = 0;
do
{
guess += .000001;
}while(guess*guess<num);
return guess;
}
int main() {
float num;
cout << "welcome.\n";
cout <<"Please enter a number >> \n";
cin >> num;
double result = sqrt(num);
cout << "The square root of " << num <<
" is "<< result;
return 0;
}