C Program to find the given polynomial P(x)=AnXn+An-1Xn-1+An-2Xn-2+A1X+A0

What is a Polynomial?

Here in this C program, we are evaluating a given Polynomial using Arrays. A Polynomial is some expression that has only Variables and Coefficients. On a Polynomial, we can do operations like Addition, Subtraction, Multiplication but not Division. Almost all fields use polynomials like Mathematics, Physics, etc. Polynomials are in so many types; the most common type of Polynomial is Quadratic Polynomial. For the calculation in a Polynomial, we have to know a basic theory 'x' raise to zero equals one.

How to calculate the value of Polynomial 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 are printing the Polynomial using another For loop.

 
 for (i=0;i<= N;i++)
     {
        if (power < 0>

Finally, calculate the sum of a Polynomial and also add the Operator based on the value of a[0] then displays 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 using printf and scanf functions.

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 Array first element.

STEP 7: Using a For loop from one to number to terms and Increment by one, we calculate the "polysum as 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 is 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;

  /*power--;*/

  printf("Given polynomial is:\n"); /* displaying the polynomial in the correct sequence */
  for (i = 0; i <= N; i++) {
    if (power < 0 > 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 output */
}
                                      

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