Data Structure Slip no-1_B

#include <stdio.h>
#include<conio.h>
// Function to evaluate a polynomial at a given 'x' value
double evaluatePolynomial(double coefficients[], int degree, double x)
{
    double result = 0.0;
    for (int i = 0; i <= degree; i++)
    {
        result += coefficients[i] * pow(x, i);
    }
    return result;
}

void main()
{
    int degree;
    clrscr();
    printf("Enter the degree of the polynomial: ");
    scanf("%d", &degree);
 
    double coefficients[degree + 1];
    for (int i = 0; i <= degree; i++)
    {
        printf("Enter coefficient for x^%d: ", i);
        scanf("%lf", &coefficients[i]);
    }
 
    double x;
    printf("Enter the value of x: ");
    scanf("%lf", &x);
 
   double result = evaluatePolynomial(coefficients, degree, x);
   printf("The result of the polynomial at x = %.2lf is %.2lf\n", x, result);
   getch();
}
 
 

No comments:

Post a Comment