Algorithm C++ programming Coding Mathematical Algorithms Newton Raphson Method

C++ Programming – Program for Newton Raphson Method

C++ Programming - Program for Newton Raphson Method - Mathematical Algorithms - Given a function f(x) on floating number x and an initial guess for root

Given a function f(x) on floating number x and an initial guess for root, find root of function in interval. Here f(x) represents algebraic or transcendental equation.

For simplicity, we have assumed that derivative of function is also provided as input.


Input: A function of x (for example x3 – x2 + 2),
derivative function of x (3×2 – 3x for above example)
and an initial guess x0 = -20
Output: The value of root is : -1.00
OR any other value close to root.
We have discussed below methods to find root in set 1 and set 2
Set 1: The Bisection Method
Set 2: The Method Of False Position

Comparison with above two methods:

In previous methods, we were given an interval. Here we are required an initial guess value of root.
The previous two methods are guaranteed to converge, Newton Rahhson may not converge in some cases.
Newton Raphson method requires derivative. Some functions may be difficult to
impossible to differentiate.
For many problems, Newton Raphson method converges faster than the above two methods.
Also, it can identify repeated roots, since it does not look for changes in the sign of f(x) explicitly
The formula:
Starting from initial guess x1, the Newton Raphson method uses below formula to find next value of x, i.e., xn+1 from previous value xn.


Input: initial x, func(x), derivFunc(x)
Output: Root of Func()

  1. Compute values of func(x) and derivFunc(x) for given initial x
  2. Compute h: h = func(x) / derivFunc(x)
  3. While h is greater than allowed error ε
    1. h = func(x) / derivFunc(x)
    2. x = x – h

Below is C++ implementation of above algorithm.

C++ Program
// C++ program for implementation of Newton Raphson Method for
// solving equations
#define EPSILON 0.001
using namespace std;
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2  + 2
double func(double x)
    return x*x*x - x*x + 2;
// Derivative of the above function which is 3*x^x - 2*x
double derivFunc(double x)
    return 3*x*x - 2*x;
// Function to find the root
void newtonRaphson(double x)
    double h = func(x) / derivFunc(x);
    while (abs(h) >= EPSILON)
        h = func(x)/derivFunc(x);
        // x(i+1) = x(i) - f(x) / f'(x)  
        x = x - h;
    cout << "The value of the root is : " << x;
// Driver program to test above
int main()
    double x0 = -20; // Initial values assumed
    return 0;


The value of root is : -1.00
READ  Detect if two integers have opposite signs

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.