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 Generate All Possible Subsets with Exactly k Elements in Each Subset
Java Program to Implement AVL Tree
Java Program for Douglas-Peucker Algorithm Implementation
Spring Cloud – Adding Angular
Serve Static Resources with Spring
Spring MVC Async vs Spring WebFlux
Java Program to Implement the Program Used in grep/egrep/fgrep
LinkedHashSet trong Java hoạt động như thế nào?
Custom Thread Pools In Java 8 Parallel Streams
Difference Between Wait and Sleep in Java
Set Interface trong Java
Default Password Encoder in Spring Security 5
Java program to Implement Tree Set
Java Program to Implement Skew Heap
A Quick Guide to Spring MVC Matrix Variables
Running Spring Boot Applications With Minikube
The Basics of Java Security
HttpClient Basic Authentication
Java Program to Implement Efficient O(log n) Fibonacci generator
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Guava – Join and Split Collections
Java Program to Implement Extended Euclid Algorithm
Introduction to the Java ArrayDeque
How to Get a Name of a Method Being Executed?
Introduction to Liquibase Rollback
Java Program to Permute All Letters of an Input String
Returning Custom Status Codes from Spring Controllers
Filtering a Stream of Optionals in Java
Convert String to int or Integer in Java
The Java 8 Stream API Tutorial
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Beans and Dependency Injection