Powered By Blogger

Tuesday, April 26, 2011

Square Root Of a Number Without built-in function


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
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);
                printf(" %6.4lf\n",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);
                printf(" +/- %6.4lf i\n",g2);
 }
 else
 {
                printf("The root of %6.4lf is",n);
printf(" +/- %6.4lf\n",g2);
 }
 getch();
}

No comments:

Post a Comment