Friday, June 28, 2013

C Program for finding Sqrt upto 3 decimal point

A c program for finding Sqrt upto 3 decimal point........Paper and Pencil way.
#include<stdio.h>
#include<stdlib.h>
void Sqrt(int n)
{
double R=0,d=0;
int i=0,p,temp=0,count=0;
 if(n<=0){printf("Not possible ");return ;}
 
 for(i=0;i<=n/2+1;i++)
 {   if(d==0)
      p=i*i;
      else
        p=(d*10+i)*i;
      if(p>n)
  {  
    R=(double)R*10+temp;
    n=n-(d*10+temp)*temp;
    d=(d*10+temp)+temp;
    if(n<=d&&count<3)
    {
     n=n*100;
     count++;
    } 
    else
     break;
      i=0;
  }
     temp=i; 
     }
printf("final result=%g",(float)R/1000);
}
int main(void)
{int i=0,data;
printf("Enter the data for Sqrt=");
scanf("%d",&data);
Sqrt(data);
return 0;
}
There is a better way by Newton Raphson Method....one may google it. It's my own work that's why i am posting. i don't think it's bad to know the basic.

No comments:

Post a Comment