Evaluate A Polynomial C Program

By | July 27, 2016

C Program To Evaluate A Polynomial

Learn How To Write a Code To Evaluate A Polynomial in C Programming Language. For Evaluation of a Polynomial, we first fetch the value of Co – Efficient and store them in the Array. This program is intimidating but is actually very simple if you try a little harder to understand it.

What is Polynomial?

A Polynomial is an expression or a mathematical equation which contains variables and constants (also known as Co – Efficients). The different terms in the expression includes the operations of Addition, Non – Negative Integer Exponent, Subtraction and Multiplication. A polynomial is nothing but an algebraic expression. It is also famously known as arithmetic expression.

Must Read: C Program To Evaluate a Postfix Expression

Method 1: C Program To Evaluate a Polynomial using Function

#include<stdio.h>
#include<math.h>

int evaluate_polynomial(int arr[], int limit, int x)
{
      int sum = 0, count;
      for(count = limit; count >= 0; count--)
      {
            sum = sum + arr[count]*pow(x, count);
      }
      return sum;
}

int main()
{
      int array[30], degree, x_val, count, result;
      printf("\nEnter the Degree of Polynomial:\t");
      scanf("%d", &degree);
      printf("\nEnter the Co - Efficients:\n");
      for(count = degree; count >= 0; count--)
      {
            printf("\nCo - Efficient of A[%d]: \t", degree);
            scanf("%d", &array[count]);
      }
      printf("\nThe Polynomial:\n\n");
      for(count = degree; count >= 0; count--)
      {
            if(array[count] != 0)
            {
                  printf("%dx^%d + ", array[count], count);
            }
      }
      printf("%d", array[count]);
      printf("\n\nEnter the Value of X:\t");
      scanf("%d", &x_val);
      result = evaluate_polynomial(array, degree, x_val);
      printf("\nEvaluation of Polynomial:\t%d\n", result);
      return 0;
}

Method 2: C Program To Evaluate a Polynomial without Function

#include<stdio.h>
#include<math.h>

int main()
{
      int array[30], degree, x_val, count, sum = 0;
      printf("\nEnter the Degree of Polynomial:\t");
      scanf("%d", &degree);
      printf("\nEnter the Co - Efficients:\n");
      for(count = degree; count >= 0; count--)
      {
            printf("\nCo - Efficient of A[%d]: \t", degree);
            scanf("%d", &array[count]);
      }
      printf("\nThe Polynomial:\n\n");
      for(count = degree; count >= 0; count--)
      {
            if(array[count] != 0)
            {
                  printf("%dx^%d + ", array[count], count);
            }
      }
      printf("%d", array[count]);
      printf("\n\nEnter the Value of X:\t");
      scanf("%d", &x_val);
      for(count = degree; count >= 0; count--)
      {
            sum = sum + array[count]*pow(x_val, count);
      }
      printf("\nEvaluation of Polynomial:\t%d\n", sum);
      return 0;
}

Method 3: C Program To Evaluate a Polynomial using Horner’s Method

#include<stdio.h>
 
int evaluate_polynomial(int array[], int limit, int degree)
{
      int value = array[0];
      for(int count = 1; count < limit; count++)
            value = (value * degree) + array[count];
      return value;
}

int main()
{
      int degree = 3;
      int array[] = {3, 4, 2, 1, 2};
      int limit = sizeof(array)/sizeof(array[0]);
      printf("\nValue of Polynomial:\t%d\n", evaluate_polynomial(array, limit, degree));
      return 0;
}

Output

C Program To Evaluate A Polynomial with and without Function and Horner's Method

If you have any compilation error or doubts in this C Program For Evaluation of Polynomial, let us know about it in the Comment Section below.

6 thoughts on “Evaluate A Polynomial C Program

  1. Rajesh Chawla

    The Horner’s method for evaluating a polynomial seems to take Hard Coded values. Will the user defined values work in Horner method like they work in the above two C Programs?

    Reply
    1. Tushar Soni Post author

      Yes! It will definitely work. The Horner’s method to evaluate a polynomial here takes a hard coded array. You can take input for the array from the User. I think it should work.

      Reply
  2. Sachin Shinde

    So many methods to solve and evaluate a polynomial expression in c programming helped to clear how and when to use functions. Also it helped to analyze how to convert functions into main programs.

    Reply
    1. Tushar Soni Post author

      I am glad that you liked it. The main reason behind posting 2 different ways of writing how to calculate and evaluate a polynomial in c is to make the programmers understand the minute difference between for and while loops as well as functional approach.

      Reply
  3. Mahesh Asalkar

    This C Program to evaluate an arithmetic expression is just to good. Thanks.

    Reply
  4. saiteja

    I implement the polynomial program and i extract the order and coefficients of the given polynomial . I enter x value to that polynomial but it is not giving the correct value.

    Reply

Let's Discuss