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:
Array to String Conversions
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Spring Data JPA Delete and Relationships
Write/Read cookies using HTTP and Read a file from the internet
Từ khóa static và final trong java
Java Program to Implement Word Wrap Problem
How to Manually Authenticate User with Spring Security
Exploring the Spring Boot TestRestTemplate
Giới thiệu thư viện Apache Commons Chain
Spring Data Java 8 Support
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Marker Interface trong Java
Introduction to Java 8 Streams
Java Program to Implement LinkedHashSet API
Spring WebClient Filters
Spring Security with Maven
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Handling Errors in Spring WebFlux
An Intro to Spring Cloud Contract
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Model, ModelMap, and ModelAndView in Spring MVC
Java Program to Solve Knapsack Problem Using Dynamic Programming
Lập trình mạng với java
New Features in Java 13
Java Program to Implement the Program Used in grep/egrep/fgrep
Introduction to Thread Pools in Java
Java Program to Perform Partial Key Search in a K-D Tree
Sending Emails with Java
Introduction to Apache Commons Text
ETags for REST with Spring
Guava CharMatcher