**C program of Newton Raphson Method** : **Newton Raphson method** is a method of approximating a root of the polynomial equation also called the method of tangents. In Newton’s method, the initial (“first”) approximation *x* = *a*_{1} is used to find a second, more accurate, approximation by drawing the tangent to the graph of *y* = *f(x*) at the point *A[a*_{1}, *f(a*_{1})] up to the intersection of the tangent with the *x*-axis. The point of intersection is

* x* = *a*_{1} – *f(a*_{1})/*f*’(*a*_{1})

and is adopted as the new value *a*_{2} of the root. By repeating this process as necessary, we can obtain increasingly accurate approximations *a*_{2}, *a*_{3}, … of the root provided that the derivative *f*’(*x*) is monotonic and preserves its sign on the segment containing *root*.

How to use the C program :

Consider an example : f(x) = x^2 – 4 as our polynomial equation. We want to calculate its root and say first approximation =6. Then maximum power of X = 2, coefficients of x^0 = -4, x^1 = 0, x^2 = 1 & first approximation =6. Now C program of Newton Raphson Method will display the iterations and root of the polynomial as output.

### C program of Newton Raphson Method :

#include<conio.h> #include<stdio.h> #include<stdlib.h> #include<math.h> int max_power,i=0,cnt=0,flag=0; int coef[10]={0}; float x1=0,x2=0,t=0; float fx1=0,fdx1=0; int main() { printf("-----------------------------------------------------------\n"); printf("----------------------Made by C code champ-----------------\n"); printf("-----------------------------------------------------------\n\n"); printf("\n\n\t C PROGRAM FOR NEWTON RAPHSON METHOD"); printf("\n\n\n\tENTER THE MAXIMUM POWER OF X = "); scanf("%d",&max_power); for(i=0;i<=max_power;i++) { printf("\n\t x^%d = ",i); scanf("%d",&coef[i]); } printf("\n"); printf("\n\tTHE POLYNOMIAL IS = "); for(i=max_power;i>=0;i--)/*printing coefficients*/ { printf(" %dx^%d",coef[i],i); } printf("\n\n\tFirst approximation x1 ----> "); scanf("%f",&x1); printf("\n\n-----------------------------------------------------------\n"); printf("\n ITERATION \t x1 \t F(x1) \t \tF'(x1) "); printf("\n-----------------------------------------------------------\n"); do { cnt++; fx1=fdx1=0; for(i=max_power;i>=1;i--) { fx1+=coef[i] * (pow(x1,i)) ; } fx1+=coef[0]; for(i=max_power;i>=0;i--) { fdx1+=coef[i]* (i*pow(x1,(i-1))); } t=x2; x2=(x1-(fx1/fdx1)); x1=x2; printf("\n\t %d \t%.3f \t %.3f\t\t%.3f ",cnt,x2,fx1,fdx1); }while((fabs(t - x1))>=0.0001); printf("\n\n\n\t THE ROOT OF EQUATION IS = %f",x2); getch(); }

We hope that you all have enjoyed the C program of Newton Raphson Method. If you have any doubts related to the program ask us in form of comments.

Mr CoderAugust 20, 2012 at 8:38 pm -testing…

Mr CoderAugust 20, 2012 at 8:39 pm -comments level two testing..

collectivaAugust 22, 2012 at 10:55 am -It is very useful for the Beginners.

Mr CoderAugust 23, 2012 at 8:53 pm -thanks!

pratiSeptember 5, 2012 at 4:01 pm -very useful:)…thanks:)

David DupeOctober 7, 2012 at 4:07 pm -Great blog but maybe you should get up and stand up

Jay-POctober 23, 2012 at 7:32 am -Where is the output screen with the root?

solicitors corbyNovember 2, 2012 at 5:53 am -lmao! My feelings exactly! What is the world coming to?

solicitors corbyNovember 2, 2012 at 4:39 pm -You made some respectable points there. I scoured on the web for the issue and located most individuals will associate with with your website.