Program to calculate the square root of the given number without using the built-in function.

Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main() // Uses Brute-Force Method
{
int s;
double x,d,n;
clrscr();
printf("Enter the number\n");
scanf("%lf",&n);
if(n>=0)
{
for(s=1;s*s<=n;s++); //calculating decimal part of the square root
s--;
for(d = 0.001;d < 1.0;d += 0.001) // calculating the fractional part
{
x = (double)s + d;
if((x*x > (double)n))
{
x = x - 0.001;
break;
}
}
printf("The square root of the given number is %.3lf\n", x);
printf("The square root as per built in function sqrt()is %.2lf", sqrt(n));
}
else
{
printf( "No square root to a negative number");
}
getch();
}

Output:
Enter the number
16
The square root of the given number is 4.000
The square root as per built in function sqrt() is 4.00
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: