Google Search

Custom Search

Sunday, February 14, 2010

Square Root of a Number Without Using Built-In Function

#include
#include

void main()
{
double n,g1,g2;
int flag=0;
clrscr();
printf("Enter the no.: ");
scanf("%lf",&n);
if(n<0)
{
n=-n;
flag=1;
}
else if(n==0)
{
printf("
The root of %6.4lf is",n);
textcolor(GREEN);
cprintf(" %6.4lf",g2);
getch();
exit(0);
}
g2=n/2;
do
{
g1=g2;
g2=(g1+n/g1)/2;
}
while((g1-g2)!=0);
if(flag==1)
{
printf("
The root of %6.4lf is",-n);
textcolor(GREEN);
cprintf(" +/- %6.4lf i",g2);
}
else
{
printf("
The root of %6.4lf is",n);
textcolor(GREEN);
cprintf(" +/- %6.4lf",g2);
}
getch();
}

No comments: