C Program to evaluate a polynomial equation


October 20, 2023, Learn eTutorial
2988

What is a Polynomial?

A Polynomial is an expression that has only Variables and Coefficients.

f(x) = ax2 + bx + c  is a polynomial equation of order 2 

where,

  • x is a single variable
  • a, b & c as its coefficients. 

On a Polynomial, we can do operations like Addition, Subtraction, and Multiplication but not Division.

Almost all fields use polynomials like Mathematics, Physics, etc. Polynomials are of many types; the most common type of Polynomial is Quadratic Polynomial.

For the calculation of a Polynomial, we have to know a basic theory 'x' raise to zero equals one.

How to evaluate a Polynomial equation using an array in C?

In this C program, we are using an Array to save the values or Coefficients of the Polynomial. The user has to input the values for the Order of Polynomial and Value of 'x' and Coefficients.

Then we have to find out the poly-sum inside a for loop using the formula.

"polySum = polySum * x + a[i];"

After that, we print the Polynomial in proper format using another for loop , by checking values of the array a[], order of the polynomial N entered by the user.

Finally, calculate the sum of Polynomial and also add the Operator based on the value of a[0] then display the final output.

ALGORITHM

STEP 1: Import the Header files into the C program to use the inbuilt functions.

STEP 2: Define the Array size and the other Variables used in the C program.

STEP 3: Accept the Order of the Polynomial from the user.

STEP 4: Accept the value of 'x' of the unknown in the Polynomial equation.

STEP 5: Accept the values for the Coefficients using a for loop.

STEP 6: Initialize the Polysum as the array's first element.

STEP 7: Using a for loop from one to number to terms and Increment by one,

we calculate the polySum = polySum * x + a[i]

STEP 8: Now Assign the power = N and print the Polynomial using a for loop.

STEP 9: If the Power is less than zero, then break the program.

STEP 10: If the array element is greater than Zero, print the sign as '+' else array element

is less than zero, print the character as '-,' else print nothing.

STEP 11: Now print the Polynomial and the Polynomial Sum using printf in C programming.

C Source Code

                                          #include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

void main()
{
  int a[MAXSIZE];
  int i, N, power;
  float x, polySum;
  printf("Enter the order of the polynomial\n"); /* enter order of polynomial */
  scanf("%d", & N);
  printf("Enter the value of x\n"); /* value of x */
  scanf("%f", & x);

  /*Read the coefficients into an array*/

  printf("Enter %d coefficients\n", N + 1);
  for (i = 0; i <= N; i++) {
    scanf("%d", & a[i]);
  }
  polySum = a[0];
  for (i = 1; i <= N; i++) {
    polySum = polySum * x + a[i]; /* calculating the polysum  */
  }
power = N;
 
    printf("Given polynomial is: \n");
    for (i = 0; i <= N; i++)
    {
        if (power < 0)
        {
            break;
        }
        /*  displaying polynomial  */
        if (a[i] > 0 & i!=0)
            printf(" + ");
        else if (a[i] < 0)
            printf(" - ");
        else
            printf(" ");
        printf("%dx^%d  ", abs(a[i]), power--);
  }
  printf("\nSum of the polynomial = %6.2f\n", polySum); /* displays the sum */
}
                                      

OUTPUT

RUN 1

Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6

Given polynomial is:
 3x^2 + 2x^1 + 6x^0 
Sum of the polynomial =  22.00

RUN 2

Enter the order of the polynomial
4
Enter the value of x
1
Enter 5 coefficients
3
-5
6
8
-9

Given polynomial is:
 3x^4   - 5x^3   + 6x^2   + 8x^1   - 9x^0
Sum of the polynomial =   3.00