C Program to Evaluate the Given Polynomial Equation
To deduce an error in polynomial equation.....
code :
#include <stdio.h>
int main() {
int order, i;
float x, sum, power;
printf("Enter the order of the polynomial: ");
scanf("%d", &order);
printf("Enter the value of x: ");
scanf("%f", &x);
float coefficients[order + 1];
printf("Enter the coefficients of the polynomial:\n");
for (i = 0; i <= order; i++) {
printf("x^%d: ", i);
scanf("%f", &coefficients[i]);
}
sum = coefficients[0];
power = 1;
for (i = 1; i <= order; i++) {
power *= x;
sum += coefficients[i] * power;
}
printf("The polynomial equation is: ");
// Print the polynomial in a user-friendly format
for (i = order; i >= 0; i--) {
if (coefficients[i] != 0) {
if (coefficients[i] > 0 && i > 0) {
printf("+ ");
} else if (coefficients[i] < 0) {
printf("- ");
if (coefficients[i] * -1 != 1) {
printf("%.2f ", fabs(coefficients[i]));
}
} else if (i > 0) {
printf("+ ");
}
if (i > 0) {
printf("%.2fx^%d ", fabs(coefficients[i]), i);
} else {
printf("%.2f", fabs(coefficients[i]));
}
}
}
printf("\nThe value of the polynomial at x = %.2f is %.2f\n", x, sum);
return 0;
}
.png)
Comments
Post a Comment