Sei sulla pagina 1di 19

Numerical Methods: 1

Program: 1
/* Write a program to calculate the following function, y=cosx.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.14
#define MAX 180
int main()
{
clrscr();
double x,y;
scanf("%lf",&x);
y=cos((PI*x)/MAX);
printf("%lf",y);
getch();
return 0;
}
Program: 2
/* Write a program to calculate the following equation, y=logx.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
int x;
double y;
scanf("%d",&x);
y=log(x);
printf("%lf",y);
getch();
return 0;
}

Program: 3
/* write a program to calculate the following equation,y=ex.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
float x,y;
scanf("%f",&x);
y=exp(x);
printf("%f",y);
getch();
return 0;
}
Program: 4
/* Write a program to find the value of the following function,y=ex+logx+sinx.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.14
#define Max 180
int main()
{
clrscr();
float x,y;
scanf("%f",&x);
y=exp(x)+log(x)+sin((PI*x)/Max);
printf("%f",y);
getch();
return 0;
}

Program: 5
/* Write a program to find the quadrate of a point. */
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int x,y;
scanf("%d%d",&x,&y);
if((x>=0) &&(y>=0))
printf(" First quadrate");
else if((x<0) &&(y>=0))
printf(" second quadrate");
else if((x<0) &&(y<0))
printf("Third quadrate");
else if((x>=0) &&(y<0))
printf(" Fourth quadrate");
getch();
return 0;
}
Program: 6
/* Write programs to test two lines are perpendicular or parallel. */
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
float x1,x2,x3,x4,y1,y2,y3,y4,m1,m2;
scanf("%f%f%f%f%f%f%f%f",&x1,&x2,&x3,&x4,&y1,&y2,&y3,&y4);
m1=(y1-y2)/(x1-x2);
m2=(y3-y4)/(x3-x4);
if(m1= =m2)
printf("the two lines are parallel");
else if((m1*m2)= =-1)
printf("the two lines are perpendicular");
getch();
return 0;
}

Program: 7
/* Write a program to calculate the distance of two points.
#include<stdio.h>
#include<conio.h>
#include<math.h>

*/

int main()
{
clrscr();
int x1,x2,y1,y2,d;
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
d=sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
printf("%d",d);
getch();
return 0;
}
Program: 8
/* If the values of the three arms of a triangle are given, then calculate the area of that
triangle. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
int a,b,c;
float s,area;
scanf("%d%d%d", &a,&b,&c);
if(((a+b)>c)&&((b+c)>a)&&((a+c)>b))
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("%f",area);
}
else
printf("There is no triangle for these given value of the arms");
getch();
return 0;
}

Program: 9
/* Write a program to find the area of a triangle for the three given points given. */
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int x1,x2,x3,y1,y2,y3;
float area;
scanf("%d%d%d%d%d%d",&x1,&x2,&x3,&y1,&y2,&y3);
area=0.5*(x1*(y1-y2)+x2*(y3-y1)+x3*(y1-y2));
if(area= =0)
printf("Three points are lies in a straight line");
else
printf("%f is the area of that traigle",area);
getch();
return 0;
}
Program: 11
/* Write a program to convert a binary number into a decimal number. */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
char str[2000];
int i,dec,len;
gets(str);
len=strlen(str);
dec=str[0]-48;
for(i=1;i<len;++i)
dec=(str[i]-48)+(2*dec);
printf("%d",dec);
getch();
return 0;
}

Program: 10
/* Write a program to convert a given decimal number into binary number. */
#include<stdio.h>
#include<conio.h>
int main()
{clrscr();
int a,i,count,b[100];
scanf("%d",&a);
i=0;
b[i]=0;
count=0;
while(a!=0)
{ if (a%2==0)
{b[i]=0;
a=(a-b[i])/2;
i++;
count++;
}
else
{ b[i]=1;
a=(a-b[i])/2;
i++;
count++;
}
}
printf("\n");
for(i=count;i>=0;--i)
printf("%d",b[i]);
getch();
return 0;
}

Numerical Methods: 2
Program: 12
/*Write a program to determine the binomial co-efficient nCr,where nCr= n/r. n-1/r-1.
n-2/r-2........n-r-1/1.
*/
#include<stdio.h>
#include<conio.h>
int main()
{clrscr();
int n,r,y;
float d;
scanf("%d%d",&n,&r);
d=1;
if(r!=0)
{ for(y=0;y<=r-1;++y)
d=d*(n-y)/(r-y);
}
printf("%f",d);
getch();
return 0;
}
Program: 19
/* This program solves a system of linear equations using simple Gaussian elimination
method. */
#include<stdio.h>
#include<conio.h>
int gauss1(int n,float a[10][10],float b[10],float x[10], int *status);
int main()
{clrscr();
int status,n,j,i;
float a[10][10],b[10],x[10];
printf("What is the size of the system (n)? \n");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
printf("\n");
for(i=1;i<=n;++i)
scanf("%f",&b[i]);
gauss1(n,a,b,x,&status);
if(status!=0)
{for(i=1;i<=n;++i)

printf("%10.6f\t",x[i]);
printf("\n");
}
getch();
return 0;
}
int gauss1(int n, float a[10][10],float b[10],float x[10], int *status)
{ int i,j,k;
float pivot,factor,sum;
for(k=1;k<=n-1;++k)
{ pivot=a[k][k];
if(pivot<0.0000001)
{ *status=0;
}
*status=1;
for(i=k+1;i<=n;++i)
{ factor=a[i][k]/pivot;
for(j=k+1;j<=n;j++)
{a[i][j]=a[i][j]-factor*a[k][j];
}
b[i]=b[i]-factor*b[k];
}
}
x[n]=b[n]/a[n][n];
for(k=n-1;k>=1;k--)
{ sum=0.0;
for(j=k+1;j<=n;++j)
sum=sum+a[k][j]*x[j];
x[k]=(b[k]-sum)/a[k][k];
}
return 0;
}

Program: 22
/* Write a program to computes the interpolation value at a specified point,
given a set of data points,using the Lagrange interpolation representation. */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int main()
{int n,i,j;
float x[MAX],f[MAX],fp,lf,sum,xp;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&f[i]);
scanf("%f",&xp);
sum=0.0;
for(i=1;i<=n;++i)
{ lf=1.0;
for(j=1;j<=n;j++)
{ if(i!=j)
lf=lf*(xp-x[j])/(x[i]-x[j]);
}
sum=sum+lf*f[i];
}
fp=sum;
printf("at x=%f is %f \n",xp,fp);
getch();
return 0;
}

Program: 23
/* This program constructs the Newton interpolation polynomial for a given
set of data points and then computes interpolation value at a specified value. */
#include<stdio.h>
#include<conio.h>
int main()
{ clrscr();
int i,j,n;
float xp,fp,sum,pi,x[10],f[10],a[10],d[10][10];
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;++i)
scanf("%f%f",&x[i],&f[i]);
for(i=1;i<=n;++i)
d[i][1]=f[i];
for(j=2;j<=n;++j)
for(i=1;i<=n-j+1;++i)
d[i][j]=(d[i+1][j-1]-d[i][j-1])/(x[i+j-1]-x[i]);
for(j=1;j<=n;++j)
a[j]=d[1][j];
scanf("%f",&xp);
sum=a[1];
for(i=2;i<=n;++i)
{ pi=1.0;
for(j=1;j<=i-1;++j)
pi=pi*(xp-x[j]);
sum=sum+a[i]*pi;
}
fp=sum;
printf("at x=%f is %f\n",xp,fp);
getch();
return 0;
}

Numerical Methods: 3
Program: 14
/* Write a program to determine the binomial coefficient nCr, where nCr=
n!/((r!(n-r)).
*/
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,r,p;
scanf("%d%d",&n,&r);
p=fact(n)/(fact(r)*fact(n-r));
printf("%d",p);
getch();
return 0;
}
int fact(int n)
{
if(n<=1)
return 1;
else
return (n*fact(n-1));
}

Program: 15
/* Write a program to solve f(x)=0 using regula falsi or false position method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EXP 0.0000001
float fal(float x)
{
return exp(-x)-x;
}
int main()
{
clrscr();
float x0,x1,z,f,g;
int r=0;
scanf("%f%f",&x0,&x1);
f=fal(x0);
g=fal(x1);
do{z=x1-((x0-x1)*(g/(f-g)));
if(f*fal(z)>=0)
x0=z;
else
x1=z;
r=r+1;
}while((abs(x1-x0)/x1)<=EXP);
printf("%f\n%d",x1,r);
getch();
return 0;
}
Program: 16
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
int x0,x1;
float p;
scanf("%d%d",&x0,&x1);
p=fabs(x1-x0);
printf("%f",p);
getch();
return 0;
}

*/

Program: 18
/* Write a program to solve f(x)=0 using iterative method.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EXP 0.0000001
float fnf(float x0)
{ return (x0*x0*x0+x0*x0)-1;
}
int main()
{ clrscr();
float x0,x1,x2;
int n=1;
scanf("%f",&x0);
do{ x2=fnf(x0);
x1=x0;
if(abs(x1-x2)<=EXP)
{ n=n+1;
}
}while(abs(x1-x2)<=EXP);
printf("%f\n%d\n",x2,n);
getch();
return 0;
}

Program: 20
/* Write a program to solve a system of a system of equations by JACOBI iteration
method. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define EXP 0.0000001
#define MAXIT 100
int jacobi(int n,float a[10][10],float b[10],float x[10],int *count,int *status);
int main()
{clrscr();
int i,j,n,count,status;
float a[10][10],b[10],x[10];
scanf("%d",&n);
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
scanf("%f",&a[i][j]);
printf("\n");
for(i=1;i<=n;++i)
scanf("%f",&b[i]);
jacobi(n,a,b,x,&count,&status);
if(status==2)
{printf("\n No convergence in %d iterations",MAXIT);
printf("\n\n");
}
else
{ printf("\n");
for(i=1;i<=n;i++)
printf("%15.6f",x[i]);
printf("\n\n Iterations=%d",count);
}
getch();
return 0;
}
jacobi(int n,float a[10][10],float b[10],float x[10],int *count,int *status)
{
int i,j,key;
float sum,x0[10];
for(i=1;i<=n;++i)
x0[i]=b[i]/a[i][i];
*count=1;
begin:
key=0;
for(i=1;i<=n;++i)
{sum=b[i];
for(j=1;j<=n;++j)

{if(i==j)
continue;
sum=sum-a[i][i]*x0[j];
}
x[i]=sum/a[i][i];
if(key==0)
{if(abs((x[i]-x0[i])/x[i])>EXP)
key=1;
}
}
if(key==1)
{ if(*count==MAXIT)
{*status=2;
}
else
{*status=1;
for(i=1;i<=n;++i)
x0[i]=x[i];
}
*count=*count+1;
goto begin;
}
getch();
return 0;
}

Program: 21
/* This program uses the subprogram GASEId to solve a system of equations by
Gauss-seidel itration method. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAXIT 500
#define EXP 0.00001
int gaseid( int n, float a[10][10],float b[10],float x[10],int *count,int *status);
int main()
{ clrscr();
float a[10][10],b[10],x[10];
int i,j,n,count,status;
scanf("%d",&n);
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
scanf("%f",&a[i][j]);
for(i=1;i<=n;++i)
scanf("%f",&b[i]);
gaseid(n,a,b,x,&count,&status);
if(status==2)
{ printf("\n No convergence in %d iterations\n",MAXIT);
printf("\n");
}
else
{for(i=1;i<=n;++i)
printf("% 0.6f",x[i]);
printf("\n Iteration= %d",count);
}
getch();
return 0;
}
int gaseid( int n, float a[10][10],float b[10],float x[10],int *count,int *status)
{ int i,j,key;
float sum,x0[10];
for(i=1;i<=n;++i)
x0[i]=b[i]/a[i][i];
*count=1;
begin:
key=0;
for(i=1;i<=n;++i)
{ sum=b[i];
for(j=1;j<=n;++j)
{ if(i==j)
continue;
sum=sum-a[i][j]*x0[j];

}
x[i]=sum/a[i][i];
if(key==0)
{if(abs((x[i]-x0[i])>EXP))
key=1;
}
}
if(key==1)
{ if(*count==MAXIT)
{*status=2;
}
else
{ *status=2;
for(i=1;i<=n;++i)
x0[i]=x[i];
}
*count=*count+1;
goto begin;
}
return 0;
}

Program: 24
/* Write a program that integrates a given function using the simpson's 1/3 rule. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x) 1-exp(-(x)/2.0)
int main()
{clrscr();
int n,m,i;
float a,b,h,sum,ics,x,f1,f2,f3;
scanf("%f",&a);
printf("\n");
scanf("%f",&b);
printf("\n");
scanf("%d",&n);
h=(b-a)/n;
m=n/2;
sum=0.0;
x=a;
f1=F(x);
for(i=1;i<=m;++i)
{ f2=F(x+h);
f3=F(x+2*h);
sum=sum+f1+4*f2+f3;
f1=f3;
x=x+2*h;
}
ics=sum*h/3.0;
printf("\n Integral from %f to %f \n",a,b);
printf("When h=%f is %f \n",h,ics);
getch();
return 0;
}

Program: 25
/* Write a program that estimates the solution of the first order differential
equation y=f(x,y) at a given point using Euler's method. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float func(float x, float y);
int main()
{clrscr();
int i,n;
float x,y,xp,h,dy;
float func(float,float);
scanf("%f %f",&x,&y);
printf("\n");
scanf("%f",&xp);
printf("\n");
scanf("%f",&h);
n=(int)((xp-x)/h+0.5);
for(i=1;i<=n;++i)
{ dy=h*func(x,y);
x=x+h;
y=y+dy;
printf("%5d %10.6f %10.6f \n",i,x,y);
}
printf("\n Value of y at x=%f is %f\n",x,y);
getch();
return 0;
}
float func(float x, float y)
{ float f;
f=2.0* y/x;
return (f);
}

Potrebbero piacerti anche