Algorithm to find square root of a number without any built-in functions in C.
#include<stdio.h>
void main()
{
int a,b,c,dividend,i;
float z,x,d;
printf("enter the no.");
scanf("%d",&a )
for(i=0;i<a/2;i++)
{
if((i*i)<=a&&a<((i+1)*(i+1)))
{
dividend = i;
printf("%d",dividend);
break;}}
d=(a/dividend);
z=(dividend+d)/2;
x=(a/z);
g=(x+z)/2;
printf("the sqr rt of the given no. is %2f",g);
}
for eg:
let the no. be 93
the no. 93 is inbetween (9*9) and (10*10) . so all the no. inbetween 81 and 100 has the sqr rt to be starting with 9 and preceded by floating points. [here dividend is 9]
let d=(a/dividend) (ie. d=93/9=10.3333333)
z=(9+10.333333)/2=9.666666667
x=93/9.66666667=9.62089655
g=(9.62089655+9.666666667)/2 = 9.64378178
so we got the answer accurate to 4 decimal points. for more accuracy repeat the steps...
i found out this nethod by trial and error method.
#include<stdio.h>
void main()
{
int a,b,c,dividend,i;
float z,x,d;
printf("enter the no.");
scanf("%d",&a )
for(i=0;i<a/2;i++)
{
if((i*i)<=a&&a<((i+1)*(i+1)))
{
dividend = i;
printf("%d",dividend);
break;}}
d=(a/dividend);
z=(dividend+d)/2;
x=(a/z);
g=(x+z)/2;
printf("the sqr rt of the given no. is %2f",g);
}
for eg:
let the no. be 93
the no. 93 is inbetween (9*9) and (10*10) . so all the no. inbetween 81 and 100 has the sqr rt to be starting with 9 and preceded by floating points. [here dividend is 9]
let d=(a/dividend) (ie. d=93/9=10.3333333)
z=(9+10.333333)/2=9.666666667
x=93/9.66666667=9.62089655
g=(9.62089655+9.666666667)/2 = 9.64378178
so we got the answer accurate to 4 decimal points. for more accuracy repeat the steps...
i found out this nethod by trial and error method.
Comments
Post a Comment