Java Program to Implement Horner Algorithm

This is a Java Program to Implement Horner Algorithm. Horner’s method is an efficient method for calculating polynomials.
Here is the source code of the Java Program to Implement Horner Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to implement Horner Algorithm
 **/
 
import java.util.Scanner;
 
public class Horner
{
    private int sum;
    /** constructor **/
    public Horner(int[] cof, int x)
    {
        sum = 0;
        calcSum(cof, x, cof.length - 1);
        display();
    }
    /** Calculate sum **/
    private void calcSum(int[] cof, int x, int N)
    {
        sum = cof[N] * x;
        for (int i = N - 1; i >= 1; i--)
            sum = (sum + cof[i]) * x;
        sum += cof[0];
    }
    public void display()
    {
        System.out.println("Evaluated sum = "+ sum);
    }
    /** main method **/
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Horner Algorithm Test\n");
        System.out.println("Enter highest power");
        int n = scan.nextInt();
        int[] arr = new int[n + 1];
        System.out.println("\nEnter "+ (n + 1) +" coefficients in increasing order");
        for (int i = 0; i <= n; i++)
            arr[i] = scan.nextInt();
        System.out.println("\nEnter x");
        int x = scan.nextInt();
        Horner h = new Horner(arr, x);
    }
}

Output:

Horner Algorithm Test
 
Enter highest power
5
 
Enter 6 coefficients in increasing order
1 2 3 4 5 6
 
Enter x
2
Evaluated sum = 321

Related posts:

REST Web service: Upload và Download file với Jersey 2.x
Hướng dẫn Java Design Pattern – Factory Method
Java Program to Implement Control Table
Custom Thread Pools In Java 8 Parallel Streams
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Stream Filter with Lambda Expression
Introduction to Spring Cloud CLI
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Permute All Letters of an Input String
Arrays.asList vs new ArrayList(Arrays.asList())
Different Ways to Capture Java Heap Dumps
A Guide to JPA with Spring
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
A Guide to Java SynchronousQueue
Java Program to implement Bit Set
Derived Query Methods in Spring Data JPA Repositories
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Toán tử instanceof trong java
Java Program to add two large numbers using Linked List
Java Program to Implement Binary Search Tree
Java Program to Perform Uniform Binary Search
Java Program to Find kth Largest Element in a Sequence
Spring Boot - Runners
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Implement Regular Falsi Algorithm
Java Program to Implement Sparse Matrix