Home >>c programs >C Program to solve polynomial equation

C Program to solve polynomial equation

C Program to solve Polynomial and Differential Equations

In this example, we will see a C program through which we can solve Polynomial and Differential equations.

In this program we will solve the polynomial equation entered by the user provided they also enter the value of the unknown variable x.

Program:

#include <stdio.h>
#include <conio.h>
float poly(float a[], int, float);
int main()
{
float x, a[10], y1;
int deg, i;
printf("Enter the degree of polynomial equation: ");
scanf("%d", °);
printf("Ehter the value of x for which the equation is to be evaluated: ");
scanf("%f", &x);
for(i=0; i<=deg; i++)
{
printf("Enter the coefficient of x to the power %d: ",i);
scanf("%f",&a[i]);
}
y1 = poly(a, deg, x);
printf("The value of polynomial equation for the value of x = %.2f is: %.2f",x,y1);
return 0;
}
/* function for finding the value of polynomial at some value of x */
float poly(float a[], int deg, float x)
{
float p;
int i;
p = a[deg];
for(i=deg;i>=1;i--)
{
p = (a[i-1] + x*p);
}
return p;
}

Output:
Enter the degree of polynomial equation: 2
Ehter the value of x for which the equation is to be evaluated: 5
Enter the coefficient of x to the power 0: 3
Enter the coefficient of x to the power 1: 5
Enter the coefficient of x to the power 2: 2
The value of polynomial equation for the value of x = 5.00 is: 78.00

No Sidebar ads