Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- int main()
- {
- int i,n,j,fact=1;
- float x[20],y[20][20],h,term=1,u,sum,xval;
- printf("Enter the value of n:");
- scanf("%d",&n);
- printf("\nEnter the elements of x: ");
- for(i=0 ; i<n ; i++){
- scanf("%f",&x[i]);
- }
- printf("Enter the elements of y: ");
- for(i=0 ; i<n ; i++){
- scanf("%f",&y[i][0]);
- }
- h=x[1]-x[0];
- printf("\nEnter x for which y is to be calculated:");
- scanf("%f",&xval);
- u = (xval - x[0] )/h;
- printf("\nh=%f u= %f",h,u);
- sum = y[0][0];
- for(j=1 ; j<n ; j++){
- for(i=0 ; i< (n-j) ; i++){
- // when j =2 then i =0 ,1 & when j =3 then i =0)
- y[i][j]=y[i+1][j-1] - y[i][j-1];
- }
- }
- printf("The table is :\n");
- for(i=0; i<n ; i++){
- printf("\t %.2f ",x[i]);
- for(j=0 ; j<(n-i)+1 ; j++){
- printf("\t%.2f,",y[i][j]);
- }
- printf("\n");
- }
- for(j=1 ; j<n ; j++){
- for(i=1 ; i<=j ; i++){
- fact = fact * i;
- }
- term = term * (u-(j-1));
- printf("\nTerm=%f",term);
- printf("\nFactorial=%d",fact);
- sum = sum + ((y[0][j] * term) / fact);
- fact = 1;
- }
- printf("\n\nfor x = %f the ultimate answer is sum = %f",xval,sum);
- return 0;
- }
- /*
- Output
- Enter the value of n:6
- Enter the elements of x: 1931 1941 1951 1961 1971 1981
- Enter the elements of y: 12 15 20 27 39 52
- Enter x for which y is to be calculated:1996
- h=10.000000 u= 6.500000The table is :
- 1931.00 12.00, 3.00, 2.00, 0.00, 3.00, -10.00, 0.00,
- 1941.00 15.00, 5.00, 2.00, 3.00, -7.00, 0.00,
- 1951.00 20.00, 7.00, 5.00, -4.00, 0.00,
- 1961.00 27.00, 12.00, 1.00, 0.00,
- 1971.00 39.00, 13.00, 0.00,
- 1981.00 52.00, 0.00,
- Term=6.500000
- Factorial=1
- Term=35.750000
- Factorial=2
- Term=160.875000
- Factorial=6
- Term=563.062500
- Factorial=24
- Term=1407.656250
- Factorial=120
- for x = 1966.000000 the ultimate answer is sum = 20.328125
- */
Add Comment
Please, Sign In to add comment