Create a function to calculate the Square Root of a Number - C Function

C examples for Function:Function Definition

Description

Create a function to calculate the Square Root of a Number

Demo Code

#include <stdio.h>

float absoluteValue (float x){
    if (x < 0)
        x = -x;// w  ww .ja v  a2  s.c  o  m

    return x;
}

float squareRoot (float x){
    const float epsilon = .00001;
    float guess = 1.0;

    while (absoluteValue (guess * guess - x) >= epsilon)
        guess = (x / guess + guess) / 2.0;

    return guess;
}

int main (void){
    printf ("squareRoot (2.0) = %f\n", squareRoot (2.0));
    return 0;
}

Result


Related Tutorials