Calculate for a given value X of the type float the numerical value of a polynomial of degree n: \(P(X) = A_nX^n + A_{n-1}X^{n-1}+ \cdots + A_1X + A_0\)
The values of n, coefficients \(A_n, \cdots , A_0\) and X will be entered on the keyboard.
Use the Horner schema that avoids exponentiation operations when calculating: \(P(X) = (( ( (A_n \times X + A_{n-1}) \times X + A_{n-2} ) \cdots ) \times X + A_0 \)
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
void main()
{
int N; /* degree */
float X; /* argument */
float A; /* coefficients */
float P; /* P */
printf("Enter the degree n ofthe polynomial : ");
scanf("%d", &N);
printf("Enter the value X of the argument : ");
scanf("%f", &X);
for(P=0.0 ; N>=0 ; N--)
{
printf("Enter coefficient A%d : ", N);
scanf("%f", &A);
P = P*X + A;
}
printf("P(%.2f)=%.2f\n", X, P);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Zig zag level order