You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
This is a Java Program to Implement Horner Algorithm. Horners method is an efficient method for calculating polynomials.
*/
/**
** 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);
}
}
/*
Enter highest power
5
Enter 6 coefficients in increasing order
1 2 3 4 5 6
Enter x
2
Evaluated sum = 321