Question #102861

As input, you are given an integer n, a double x, followed by n+1 doubles a_n, a_{n-1}, ..., a_0. You are to print the value of the polynomial a_0 + a_1x+a_2x^2+...+a_nx^n.

Expert's answer

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  int n;
  double x;
  double a_[50];

  double sum=0;

  cin >> n;
  cin >> x;

  for (int i=n;i>=0;i--)
  {
      cin>>a_[i];
      sum=sum+(a_[i]*pow(x,i));
  }

  cout << sum;
}
LATEST TUTORIALS
APPROVED BY CLIENTS