C code using Forward Interpolation

Problem: The population of a town is given below as thousands
Year : 1891 1901 1911 1921 1931
Population : 46 66 81 93 101

Find the population of 1895 ?

 

Code:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
  float x[20],y[20],f,s,h,d,p;
  int j,i,n;
  printf("enter the value of n :");
  scanf("%d",&n);
  printf("enter the elements of x:");
  for(i=1;i<=n;i++)
   {
        scanf("\n%f",&x[i]);
         }
                   printf("enter the elements of y:");
               for(i=1;i<=n;i++)
               {
              scanf("\n%f",&y[i]);
                                }
  h=x[2]-x[1];
  printf("Enter the value of f(to findout value):");
  scanf("%f",&f);
s=(f-x[1])/h;
p=1;
d=y[1];
for(i=1;i<=(n-1);i++)
 {
                   for(j=1;j<=(n-i);j++)
                    {
                          y[j]=y[j+1]-y[j];

                    }
                    p=p*(s-i+1)/i;
                    d=d+p*y[1];
 }
printf("For the value of x=%6.5f THe value is %6.5f",f,d);
 getch();
}

Output:

forward interpolation

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *