Mega Code Archive

 
Categories / C / Beginners
 

Square root of a number without using Built-In Function

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