C > Beginners Lab Assignments
Square root of a number without using Built-In Function
Square root of a number without using Built-In Function #include
#include
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(); }
C Codes
Beginners
C on Unix
Code Snippets
Data Structures
File Operations
Games Graphics
Gnu-Linux
Hardware
Mathematics
Miscellaneous
Small Programs
Sorting