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:
OAuth2 Remember Me with Refresh Token
Java Program to Implement Bucket Sort
Java Program to Implement Splay Tree
Java Program to Implement Word Wrap Problem
An Intro to Spring Cloud Vault
Converting a List to String in Java
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
How to Implement Caching using Adonis.js 5
Lớp Collectors trong Java 8
Java Program to Implement Adjacency List
How to Read a File in Java
Java Program to Compare Binary and Sequential Search
Tips for dealing with HTTP-related problems
Java Program to Implement Binomial Tree
Introduction to Spring Cloud OpenFeign
Java Program to Implement SynchronosQueue API
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Marker Interface trong Java
Java – Reader to InputStream
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Automatic Property Expansion with Spring Boot
Java Program to Implement WeakHashMap API
Dockerizing a Spring Boot Application
Java Program to Implement Variable length array
Giới thiệu về Stream API trong Java 8
Java Program to Implement Pollard Rho Algorithm
Java Program to Print only Odd Numbered Levels of a Tree
A Guide to BitSet in Java
New Stream Collectors in Java 9
JUnit 5 for Kotlin Developers
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Recommended Package Structure of a Spring Boot Project