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> int main() { float x[10],y[10],temp=1,f[10],sum,p; int i,n,j,k=0,c; printf("\nhow many record you will be enter: "); scanf("%d",&n); for(i=0; i<n; i++) { printf("\n\nenter the value of x%d: ",i); scanf("%f",&x[i]); printf("\n\nenter the value of f(x%d): ",i); scanf("%f",&y[i]); } printf("\n\nEnter X for finding f(x): "); scanf("%f",&p); for(i=0;i<n;i++) { temp = 1; k = i; for(j=0;j<n;j++) { if(k==j) { continue; } else { temp = temp * ((p-x[j])/(x[k]-x[j])); } } f[i]=y[i]*temp; } for(i=0;i<n;i++) { sum = sum + f[i]; } printf("\n\n f(%.1f) = %f ",p,sum); getch(); }
Output: