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:
Spring Boot - Cloud Configuration Client
Java Program to Implement Network Flow Problem
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Tránh lỗi NullPointerException trong Java như thế nào?
Getting the Size of an Iterable in Java
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement Skip List
Java Program to Perform Polygon Containment Test
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Sparse Array
Case-Insensitive String Matching in Java
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
HttpClient 4 – Follow Redirects for POST
Send email with authentication
Java Program to Implement Kosaraju Algorithm
Spring Security Custom AuthenticationFailureHandler
Java Program to Perform the Unique Factorization of a Given Number
Working with Network Interfaces in Java
Spring Boot - Tomcat Deployment
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Giới thiệu Google Guice – Injection, Scope
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Construct an Expression Tree for an Prefix Expression
Add Multiple Items to an Java ArrayList
Spring MVC Tutorial
Encode/Decode to/from Base64
Spring Boot - Tracing Micro Service Logs
Immutable ArrayList in Java
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Removing all Nulls from a List in Java
A Guide to Queries in Spring Data MongoDB