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:
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Implement Suffix Array
Generic Constructors in Java
Using JWT with Spring Security OAuth (legacy stack)
Java Program to Implement Brent Cycle Algorithm
Java Program to Implement Control Table
Filtering a Stream of Optionals in Java
Java 14 Record Keyword
Spring 5 WebClient
Java Program to Implement Hash Trie
Spring Boot - Database Handling
Java Program to Implement Meldable Heap
Mix plain text and HTML content in a mail
Java Program to Find Maximum Element in an Array using Binary Search
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement Self Balancing Binary Search Tree
Connect through a Proxy
Java Program to Find All Pairs Shortest Path
Java – InputStream to Reader
Java Program to Implement Patricia Trie
Convert Character Array to String in Java
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Implement Gale Shapley Algorithm
Spring Boot Configuration with Jasypt
A Guide to EnumMap
Java Switch Statement
Java Program to Implement Counting Sort
Receive email by java client
Refactoring Design Pattern với tính năng mới trong Java 8
Java Program to Implement Bresenham Line Algorithm
Uploading MultipartFile with Spring RestTemplate