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:
4 tính chất của lập trình hướng đối tượng trong Java
Basic Authentication with the RestTemplate
Mapping a Dynamic JSON Object with Jackson
Java Program to Solve a Matching Problem for a Given Specific Case
Split a String in Java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Documenting a Spring REST API Using OpenAPI 3.0
Servlet 3 Async Support with Spring MVC and Spring Security
Giới thiệu java.io.tmpdir
Tạo số và chuỗi ngẫu nhiên trong Java
Spring Boot - Exception Handling
Java Program to implement Dynamic Array
Java Multi-line String
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement EnumMap API
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Spring Security 5 – OAuth2 Login
Custom Exception trong Java
Lớp HashMap trong Java
Handling URL Encoded Form Data in Spring REST
Using a Spring Cloud App Starter
LinkedHashSet trong java
Biến trong java
Java Program to Implement Find all Back Edges in a Graph
Java Program to implement Circular Buffer
How to Return 404 with Spring WebFlux
Adding Shutdown Hooks for JVM Applications
How to use the Spring FactoryBean?
Stack Memory and Heap Space in Java
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree