Thursday 15 March 2012

how calculate square root of a number

#include<stdio.h>
float root(float num);
void main()
{
float n,ans;
printf("enter the number:");
scanf("%f",&n);
ans=root(n);
printf("square root of %f=%f",n,ans);
}
float root(float num)
{
if(num>=0)
{
float x=num;
int i;
for(i=0;i<20;i++)
{
x=(((x*x)+num)/(2*x));
}
return x;
}
}

No comments:

Post a Comment