Sunday, April 8, 2012

Square Root of a number by using simple calculations

This is a very simple program with simple logic
to find square root of a number. By simple modifications you can find also nth root of a number also.

#include<stdio.h>
#include<conio.h>
main()
 {
  float a,b,e=0.00001,p,k;
  clrscr();
    textcolor(GREEN);
 do {
     printf("         ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
");
     printf("         xDB     PROGRAM TO FIND SQUARE ROOT OF A NUMBERxDB
");
     printf("         ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
");
    cprintf("
ENTER A NUMBER(-1 to Quit) :");
    scanf("%f",&k);

  a=k;p=a*a;
  while(p-k>=e)
   {
    b=(a+(k/a))/2;
    a=b;
    p=a*a;
   }
  printf("SQUARE ROOT IS =  %f
",a);
  getch();
  clrscr();
 }while(k!=-1);
  getch();
 }

No comments:

Post a Comment