The basic algorithm of C language 11-Newton iterative method to find square root

Source: Internet
Author: User
Tags square root

Iterative method
/*
==================================================================
Title: Newton Iterative method to find the square root of a! Iterative formula: xn+1= (XN+A/XN)/2.
==================================================================
*/
#include <stdio.h>
#include <math.h>
Main ()
{
float a,x0,x1;
int flag=1;
while (flag)
{
printf ("a=");
scanf ("%f", &a);
if (a>=0)
flag=0;
Else
printf ("You cannot enter the number of square root, please try again!") \n_______________________________\n\n ");
}
X0=1;
x1=x0+1;
while (Fabs (x0-x1) >=1e-10)
{
x0=x1;
x1=0.5* (x0+a/x0);
}
printf ("The square root of the%.2f is:%.2f\n", a,x1);
}
/*
==================================================================
Comment: The essence is iterative method, first enter the number a that meets the condition, give x0 and X1 assignment, make the absolute value of the difference
Greater than 10-10 times. Use an iterative formula to make the difference between x0 and X1 when the errand is greater than or equal to 10-10 times
Less than 10-10 times. The square root is x1 or x0.
==================================================================
*/







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The basic algorithm of C language 11-Newton iterative method to find square root

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.