Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how can i write code for square roots of a number

+23 votes
asked May 7 by Arthur Lukuwi (220 points)

7 Answers

+1 vote
answered May 7 by ProAstroShan (160 points)
It is as simple as raising the number to the power of half. For example in python, you could:

num_squared = num ** 0.5
0 votes
answered May 11 by Peter Minarik (101,360 points)
Most languages have libraries that offer such functionality.

What language are you looking for?

Writing a function that calculates the square root without using library functions is a different kind of beast. :)
+2 votes
answered May 14 by Alec (240 points)
depends on the language, but in python you can use sqrt() to achieve this.
0 votes
answered May 29 by Mahmoud Metwally (140 points)
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double number = 49;

    cout << sqrt(number) << endl;

    return 0;
}
0 votes
answered 6 days ago by Jing (170 points)

Newton-Raphson Method 

#include <stdio.h>
#include <math.h>

// Newton-Raphson sqrt implementation
double sqrt_newton(double x, double tolerance, int maxIter) {
    if (x < 0) return NAN; // Handle negative input
    if (x == 0 || x == 1) return x; // Edge cases
    
    double y = x / 2.0; // Initial guess
    
    for (int i = 0; i < maxIter; i++) {
        double y_new = (y + x / y) / 2;
        
        if (fabs(y_new - y) < tolerance || fabs(y_new * y_new - x) < tolerance) {
            return y_new;
        }
        
        y = y_new;
    }
    
    return y;
}

// Test function
int main() {
    double test_values[] = {0, 1, 2, 4, 9, 16, 25, 100, 0.25, 0.5};
    int num_tests = sizeof(test_values) / sizeof(test_values[0]);
    
    printf("Testing sqrt_newton implementation:\n");
    printf("Value\t\tNewton\t\tMath sqrt\t\tDifference\n");
    printf("--------------------------------------------------------\n");
    
    for (int i = 0; i < num_tests; i++) {
        double x = test_values[i];
        double newton_result = sqrt_newton(x, 1e-10, 100);
        double math_result = sqrt(x);
        double diff = fabs(newton_result - math_result);
        
        printf("%.6f\t\t%.10f\t\t%.10f\t\t%.2e\n", 
              x, newton_result, math_result, diff);
    }
    
    return 0;
}

0 votes
answered 4 days ago by chaithanya vardhan N (150 points)
import math

num = float(input("Enter a number: "))
result = math.sqrt(num)

print("Square root =", result)
+1 vote
answered 17 hours ago by max (180 points)
for C

#include <math.h>

int main(){

int num = 4, res;

res = sqrt(num);

printf("%d", res);

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...