#include<iostream.h>
#include<conio.h>
#include<math.h>
double a[20][20];
int n;
void table()
{
cout<<"\nEnter the number of terms=";
cin>>n;
cout<<"\nEnter First x's value=\n";
for(int i=0;i<n;i++)
{
cout<<"\nx"<<i<<"=";
cin>>a[i][0];
}
cout<<"\nEnter First y's value=\n";
for(i=0;i<n;i++)
{
cout<<"\ny"<<i<<"=";
cin>>a[i][1];
}
int k=n-1;
for(int j=2;j<n+1;j++)
{
int c=0;
for(i=0;i<=k;i++)
if(c>k-1)
cout<<"*";
else
{
a[c][j]=a[i+1][j-1]-a[i][j-1];
c++;
}
}
k--;
clrscr();
cout<<"\n Newton's Forward formula=\n";
for(i=0;i<n;i++)
{
for(j=0;j<n-i+1;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
}
int fact(int x)
{
if(x==0 || x==1)
return 1;
else
return x*fact(x-1);
}
void main()
{
clrscr();
float x,u;
table();
cout<<"\nEnter the value of f(x) for x=";
cin>>x;
u=float(x-a[0][0])/(a[1][0]-a[0][0]);
float m=1,b[20];
float s=a[0][1];
for(int i=0;i<n-1;i++)
{
m=m*(u-i);
b[i]=m;
}
for(i=1;i<n;i++)
{
s=s+(a[0][i+1]*b[i-1])/fact(i);
}
cout<<"\nResult="<<s;
getch();
}
#include<conio.h>
#include<math.h>
double a[20][20];
int n;
void table()
{
cout<<"\nEnter the number of terms=";
cin>>n;
cout<<"\nEnter First x's value=\n";
for(int i=0;i<n;i++)
{
cout<<"\nx"<<i<<"=";
cin>>a[i][0];
}
cout<<"\nEnter First y's value=\n";
for(i=0;i<n;i++)
{
cout<<"\ny"<<i<<"=";
cin>>a[i][1];
}
int k=n-1;
for(int j=2;j<n+1;j++)
{
int c=0;
for(i=0;i<=k;i++)
if(c>k-1)
cout<<"*";
else
{
a[c][j]=a[i+1][j-1]-a[i][j-1];
c++;
}
}
k--;
clrscr();
cout<<"\n Newton's Forward formula=\n";
for(i=0;i<n;i++)
{
for(j=0;j<n-i+1;j++)
cout<<"\t"<<a[i][j];
cout<<"\n";
}
}
int fact(int x)
{
if(x==0 || x==1)
return 1;
else
return x*fact(x-1);
}
void main()
{
clrscr();
float x,u;
table();
cout<<"\nEnter the value of f(x) for x=";
cin>>x;
u=float(x-a[0][0])/(a[1][0]-a[0][0]);
float m=1,b[20];
float s=a[0][1];
for(int i=0;i<n-1;i++)
{
m=m*(u-i);
b[i]=m;
}
for(i=1;i<n;i++)
{
s=s+(a[0][i+1]*b[i-1])/fact(i);
}
cout<<"\nResult="<<s;
getch();
}
No comments:
Post a Comment