Square Root

 Square Root


/*#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n;
float sqrt, temp;
printf("Enter a value whose square root has to be calculated\n");
scanf("%d",&n);
if(n<=0)
{
printf("Sqrt can’t be determined");
exit(0);
}
sqrt = n / 2;
temp = 0;
while(sqrt != temp)
{
temp = sqrt;
sqrt = ( n/temp + temp) / 2;
}
printf("The square root of %d is %f", n, sqrt);
}
*/


#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
    int s;
    double x,d,n;

    printf("Enter the number:\n");
    scanf("%lf",&n);
        if(n>=0)
        {
            for(s=1;s*s<=n;s++)
                s--;
            for(d=0.001; d<1.0; d+=0.001)
            {
                x=(double)s+d;
                if(x*x>n)
                {
                    x=x-0.001;
                    break;
                }
            }
            printf("The square root of a given number is %.3lf\n", x);
        }
        else
        {
            printf("No square root to negative numbers\n");
        }
}



s

0 Comments