How can I find square root using c program?
Hi Friends,
I am coding on C program and in which I have to fin the square root of the given number. I have tried the "sqrt" function to calculate square root. But I run my program I get lots of error for this. I am getting frustrated because of this.
Is anyone posses C program to calculate the square root of specified number? Your help would be greatly appreciable.
Re: How can I find square root using c program?
Refer the below c program for sqrt root . Run this program on you system and let me know if it is executed successfully or not.
Quote:
#include <stdio.h>
#include <math.h>
int main(void)
{
double p = 25.0, rt;
rt = sqrt(p);
printf("Square root of is %f", rt);
return 0;
}
Re: How can I find square root using c program?
Hi,
Try with following function code, use the this logic to calculate the square root using the c language. I hope it will definitely work for you.
Quote:
double squroot( const double fg)
{
double n = fg / 2.0;
double lstX = 0.0;
while(n != lstX)
{
lstX = n;
n = (n + fg/n) / 2.0;
}
Re: How can I find square root using c program?
Square root program:
Quote:
main()
{
float p,q,f=0.00001,a,l;
clrscr();
printf(" Square root program");
p=l;a=p*p;
while(a-l>=f)
{
q=(p+(l/p))/2;
p=q;
q=p*p;
}
printf("square root = %f",p);
getch();
}
while(l!=-1);
getch();
}
Re: How can I find square root using c program?
Hello Friend,
The program to calculate the square root is very complicated than calculating the square of any given number. You should use "while" loop to check conditions for this program. It will be more complicated for you if you use the "for loop". see below:
Quote:
while(nm != 0)// here nm is the which is calculated by division of two
{
// body of loop
}
Re: How can I find square root using c program?
Hi,
Have you tried below code to calculate square root using c program? Please try this:
Quote:
#include <math.h>
#include <stdio.h>
Void main()
{
float q = 64.0, w;
w = sqrt(q);
printf("Square root of given number =%f", w);
getch();
}