+ 4

How to write a program to find sqaure root of a number without using math library?

5th Aug 2016, 5:13 PM
ASTHA SINGH
ASTHA SINGH - avatar
8 Antworten
+ 2
There are many methods of computing square roots. https://en.m.wikipedia.org/wiki/Methods_of_computing_square_roots
6th Aug 2016, 1:19 AM
WPimpong
WPimpong - avatar
+ 1
Oh shit. I forgot were in java. i styled this code in c++ syntax by mistake. just make the necessary adjustments to main ()
23rd Sep 2016, 2:43 AM
Devin Custodio
Devin Custodio - avatar
+ 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); } }
23rd Sep 2016, 2:55 AM
Devin Custodio
Devin Custodio - avatar
+ 1
It works for me and very accurate result -> result=1; for(int i=1; i<=10; i++) { result=0.5*(result+n/result); }
18th Dec 2018, 8:22 PM
Praveen Soni
Praveen Soni - avatar
0
i guess math.sqrt() is the only way to find it
10th Aug 2016, 6:20 AM
Abirohman
Abirohman - avatar
0
make your own library
10th Aug 2016, 7:01 PM
Mijokh Mk 7
Mijokh Mk 7 - avatar
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); }}
18th Aug 2016, 4:21 PM
Suraj Vishwakarma
Suraj Vishwakarma - avatar
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; }
23rd Sep 2016, 2:42 AM
Devin Custodio
Devin Custodio - avatar