News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Function for square root without using sqrt() function

Started by thiruvasagamani, Sep 22, 2008, 12:03 PM

Previous topic - Next topic

thiruvasagamani

Write a function for square root without using sqrt() function

#include <stdio.h>
#include <conio.h>

float sqroot( float x)
{
float a , b;
a = x; // copy given value to 'a'
do
{
b = a; // copy value of 'a' to 'b' before 'a' is modify
a = (a + x/a) / 2; // modify 'a' value until we reach sqroot result
}
while( a!= b); // execute loop until a == b
return( a); // 'a ' or 'b' is sqroot of 'x'
}

main()
{
float x;
float sqroot( float);
printf( enter any number \n");
scanf("%f", &x);
if( x > 0)
printf(" SQRT(%f) = %f\n", sqroot( x)); // display sqrt(49) = 7
else
if(x < 0)
printf(" SQRT(%f) = %fi \n", sqroot( -x)); // display sqrt( -49) = 7i
else
printf(" square root is 0 \n");
getch();
}

o/p:

If you enter 'x' values as 49 output is 7
If 'x' value is -25 output is 5i
If 'x' value is 256 output is 16
Thiruvasakamani Karnan


Ashok G

Thanks it is just easy and simple to be integrated with any language

Hubertglaft