Computer Science, asked by gauri23banshetti, 2 months ago

Write a ‘C’ program to evaluate a given polynomial using function. (Use array).

Answers

Answered by dreamrob
1

Program:

#include<stdio.h>

#include<conio.h>

#include<math.h>

int cal(int A[], int n, int x)

{

int sum = 0;

for(int i = n; i >= 0; i--)

{

 sum = sum + (A[i] * pow(x , i));

}

return sum;

}

int main()

{

int n;

printf("Enter the degree of polynomial : ");

scanf("%d", &n);

int A[n];

printf("\nEnter the coefficient of polynomial : \n");

for(int i = n; i >= 0; i--)

{

 printf("Enter the coefficient of x^%d : ", i);

 scanf("%d", &A[i]);

}

printf("The polynomial is : ");

for(int i = n; i >= 0; i--)

{

 if(A[i] != 0)

 {

  if(i != 1 && i != 0)

  {

   printf("%dx^%d + ",A[i],i);

  }

  else if(i == 1)

  {

   printf("%dx + ",A[i]);

  }

  else

  {

   printf("%d", A[i]);

  }

 }

}

int x;

printf("\n\nEnter the value of x : ");

scanf("%d",&x);

printf("After evaluation we get : %d", cal(A, n, x));

return 0;

}

Output:

Enter the degree of polynomial : 3

Enter the coefficient of polynomial :

Enter the coefficient of x^3 : 1

Enter the coefficient of x^2 : 2

Enter the coefficient of x^1 : 3

Enter the coefficient of x^0 : 4

The polynomial is : 1x^3 + 2x^2 + 3x + 4

Enter the value of x : 2

After evaluation we get : 26

Similar questions