NEWTONS BACKWARD DIFFERENCE INTERPOLATION IN C

//PROGRAM TO FIND NEWTONS BACKWARD DIFFERENCE INTERPOLATION
#include<stdio.h>
#include<stdlib.h>
#define MAX 15
int factorial (int value);
int main ()
{
 int n,i,j,m;
 float x[MAX],y[MAX],differ[MAX][MAX],p,xvalue,yvalue,hvalue,product,sum;
 
 puts("Number of entries of x :\n");
 scanf("%d",&n);
 
 for (i=0;i<n;i++)
 {
   puts("Enter the value of x and corresponding value of y :\n");
   scanf("%f%f",&x[i],&y[i]);
 }
    puts("Enter the value of x you want to find :\n");
 scanf("%f",&xvalue);
    
    if (xvalue<x[0]||xvalue>x[n-1])
 {
  puts("Value lies outside the given values of x .");
  return 0;
    }   
    else
    {
     printf("\n\nNEWTON'S BACKWARD DIFFERENCE INTERPOLATION");
     for(j=0;j<n-1;j++)
     {
      for(i=j+1;i<n;i++)
      {
        if(j==0)
                   differ[i][j]=y[i]-y[i-1];   
     else
      differ[i][j]=differ[i][j-1]-differ[i-1][j-1]; 
  }
        }
 printf("\n\nx\ty");
 for(i=1;i<n;i++)
 {
  printf("[%d] or -diff",i);
    }
 puts("\n\n");
 for(i=0;i<n;i++)
 {
  printf("%3f%3f",x[i],y[i]);
  for(j=0;j<i;j++)
  {
   printf("%4f",differ[i][j]);
  }
  puts("\n\n"); 
 }  
 hvalue=x[1]-x[0];
 p=(xvalue-x[n-1])/hvalue;
 sum=y[n-1];
  for(i=0;i<n-1;i++)
 {
  product=1;
     for(j=0;j<=i;j++)
        {
        product=product*(p+j); 
 
  }
    m=factorial(i+1);
     sum=sum+(differ[n-1][i]*product)/m;
  }
    printf("Interpolated value is : %f",sum);
  }
  return 0;
}
int factorial (int value)
{
 int i,temp=1;
    for(i=value;i>=1;i--)
    {
     temp=temp*i;
 }
 return(temp);
}

Comments