nacp programs (1)
TRANSCRIPT
-
8/2/2019 NACP Programs (1)
1/79
Numerical Analysis And ComputProgramming
Programs
Submitted By:
Mohd.Yawar Nihal Siddiqui
10-CSS-40B.Tech, Computer Engineering
h Semester
-
8/2/2019 NACP Programs (1)
2/79
Table of Contents
PROGRAM TO CALCULATE SUM AND AVERAGE OF 100 NATURAL NUMBERS ..........
PROGRAM TO FIND PRODUCT OF 1ST 'N' NATURAL NUMBERS ..............................
FIBBONACCI SERIES ........................................................................................
GENERATING PRIME NO. BETWEEN 1 AND 100 ....................................................
GENERATE PRIME NOS. BETWEEN 1 AND 100 EXCEPT THOSE DIVISIBLE BY 5 ........
TO SORT A LIST OF 5 NUMBERS IN ASCENDING ORDER .......................................
CALCULATION OF THE VALUE OF nCr .................................................................
SUM OF DIGITS OF A GIVEN NUMBER .................................................................
CALCULATION OF TAXABLE INCOME ...................................................................
CALCULATION OF SINE SERIES .........................................................................
CALCULATION OF COSINE SERIES ......................................................................
PROGRAM TO FIND OUT AVERAGE MARKS OF STUDENTS ...................................
PROGRAM TO REVERSE THE DIGITS OF A NUMBER AND FIND THE SUM OF ITS DIGIT
STANDARD DEVIATION AND VARIATION ............................................................
CONVERT BINARY NUMBER TO DECIMAL NUMBER ..............................................
CONVERT DECIMAL NUMBER TO BINARY NUMBER ..............................................
ADDITION OF MATRIX ......................................................................................
MULTIPLICATION OF MATRIX ............................................................................
BISECTION METHOD .........................................................................................
NEWTON RALPHSON ........................................................................................
REGULA-FALSE METHOD ..................................................................................
NEWTON GREGORY FORWARD INTERPOLATION ..................................................
NEWTON GREGORY BACKWARD INTERPOLATION ................................................
LAGRANGE METHOD OF INTERPOLATION ...........................................................
-
8/2/2019 NACP Programs (1)
3/79
NEWTON DIVIDED DIFFERENCE METHOD ...........................................................
BESSEL'S METHOD OF INTERPOLATION ..............................................................
STIRLING METHOD OF INTERPOLATION ..............................................................
TRAPEZOIDAL RULE ........................................................................................
SIMPSION 1/3 RULE .........................................................................................
SIMPSION 3/8 RULE ..........................................................................................
BOOLS RULE ...................................................................................................
WEDDLE'S RULE ..............................................................................................
GAUSS ELIMINATION METHOD .........................................................................
GAUSS JORDAN METHOD .................................................................................
GAUSS SEIDAL METHOD ..................................................................................
CURVE FITTING - STRAIGHT LINE ......................................................................
RUNGA - KUTTA METHOD .................................................................................
ROMBERG METHOD ...........................................................
NUMERICAL DOUBLE INTEGRATION..............................................
-
8/2/2019 NACP Programs (1)
4/79
1. Write a program in C to find sum and average of square of first 100
natural numbers.
/* PROGRAM TO CALCULATE SUM AND AVERAGE OF 100 NATURAL NUMBERS*/
# include#include void
main( )
{
int i,
n=100,sum=0, p;
float avg;
for
(i=1;i
-
8/2/2019 NACP Programs (1)
5/79
2. Write a program to find to find the product of first n natural numbers.
/*PROGRAM TO FIND PRODUCT OF 1ST 'N' NATURAL NUMBERS*/
#include
#include
void
main(){
int i,n;
long
int
prod;
clrscr(
);
printf("\nEnter the value of
n:\t"); scanf("%d",&n);i=1,prod=1;
a5:
i=i+1;
prod=
prod*
i;
if(i0);
for (j=i-
1;j>=0;j--)
{
printf ("The binary number is
%d",rem[j]);
}
}
OUTPUT:
Enter the number= 123The binary number is 1111011
-
8/2/2019 NACP Programs (1)
31/79
18. Write a program in C to add two rectangular matrices.
/* ADDTION OF MATRIX */
#include#include
void
main()
{
int mat1[10][10],mat2[10]
[10],mat3[10][10]; int i,j,m,n;
clrscr();
printf ("\n The number of rows
are="); scanf ("%d",&m);
printf ("\n The number of columns
are="); scanf ("%d",&n);
for
(i=0;i
-
8/2/2019 NACP Programs (1)
32/79
}}
for
(i=0;i
-
8/2/2019 NACP Programs (1)
33/79
19. Write a program to perform multiplication in matrices.
/*MULTIPLICATION OF MATRIX*/
#include
#include
void
main()
{int m,n,p,q,i,j,k;
int a[10][10],b[10]
[10],c[10][10]; clrscr();
printf("\nenter no. of row and col of
matrix a:"); scanf("%d%d",&m,&n);
printf("\nenter no. of row and col of
matrix b:"); scanf("%d%d",&p,&q);
if(n!=p)
{ printf("\nmatrix can't be
multiplied\n");goto end;
}
printf("\nenter matrix
a\n"); for(i=0;i
-
8/2/2019 NACP Programs (1)
34/79
c
h
(
)
;
}
-
8/2/2019 NACP Programs (1)
35/79
Output:
Enter no. of row and col of matrix a:3 3Enter no. of row and col of matrix b:3 2
Enter matrix a0 1 2
1 2 32 3 4
Enter matrix b1 -2-1 02 -1
The product of matrix is:3 -25 -57 -8
-
8/2/2019 NACP Programs (1)
36/79
20. Bisection Method
/*BISECTION METHOD*/
#include
#include
#include
void
main()
{ float fun(float m);
float
x1,x2,x3,p,q,r
; int i=0;clrscr();
l10: printf("\nequation:x*(exp(x)-1) ");
printf("\nenter the app value of
x1,x2:"); scanf("%f %f",&x1,&x2);
if(fun(x1)*fun(x2)>0){ printf("\n wrong values entered...enter
again:\n"); goto l10;}
else
printf("\n the root lies b/w %f & %f",x1,x2);
printf("\n n x1 x2
x3
f(x3)");
l15:
x3=(x1+x2)/2;
p=fun(x1);
q=fun(
x2);
r=fun(
x3);
i=i++;
printf("\n%d %f %f %f %f
%f
if((p*r
)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))
-
8/2/2019 NACP Programs (1)
37/79
f(x1) f(x2)
%f",i,x1,x2,x3,p,q,r);n x1 x2 x3 f(x1) f(x2) f(x3)
1 0.500000 1.000000 0.750000 -0.175639 1.718282 0.5877502 0.500000 0.750000 0.625000 -0.175639 0.587750 0.1676543 0.500000 0.625000 0.562500 -0.175639 0.167654 -0.0127824 0.562500 0.625000 0.593750 -0.012782 0.167654 0.075142
5 0.562500 0.593750 0.578125 -0.012782 0.075142 0.030619
6 0.562500 0.578125 0.570312 -0.012782 0.030619 0.008780
7 0.562500 0.570312 0.566406 -0.012782 0.008780 -0.0020358 0.566406 0.570312 0.568359 -0.002035 0.008780 0.0033649 0.566406 0.568359 0.567383 -0.002035 0.003364 0.00066210 0.566406 0.567383 0.566895 -0.002035 0.0006-0.000687
Root of the equation is 0.566895
-
8/2/2019 NACP Programs (1)
38/79
21.Newton Ralphson Method.
/*NEWTON RALPHSON*/
#include#include
#include
void
main()
{float
f(float a);
float
df(float a);
int i=1;
float x0,x1,p,q;
float error =0.0001,delta
=0.001; clrscr();
printf("\n\nThe equation is X^3+1.2X^2-
5X-7.2"); printf("\nenter the initial
value of x0:");
scanf("%f",&x0);
printf("\n i x0 x1 f(x0) df(x0)\n ");
if (fabs (df(x0))
-
8/2/2019 NACP Programs (1)
39/79
return(g1);
}
Output:
The equation isX^3+1.2X^2-5X-7.2Enter the initial valueof x0:2
i x0 x1 f(x0) df(x0)
2 2.000000 2.372881 -4.400000 11.800000
3 2.372881 2.313010 1.052938 17.586615
4 2.313010 2.311227 0.029603 16.601265
5 2.311227 2.311225 0.000026 16.572248
The root of equation X^3+1.2X^2-5X-7.2 is 2.311227
-
8/2/2019 NACP Programs (1)
40/79
22.Regula falsi Method (Method of false position).
/*REGULA-FALSE METHOD */
#include
#include#include
float f(float x)
{return(3*x-
cos(x)-1);
}void main()
{ float f(float x);
double
x1,x2,m;
int c=0;
clrscr();printf("\n enter the first
approximation :"); scanf("%f",&x1);
printf("\n enter the second
approximation :"); scanf("%f",&x2);
if(f(x1)*f(x
2)=0.0001)
{
c++;
if(f(x1)*f(
m)
-
8/2/2019 NACP Programs (1)
41/79
approximation :"); getch();}
OUTPUT:
Enter the first approximation :0
Enter the secondapproximation: 1The 1st iteration is 0.605959The 2nd iteration is 0.607057The 3rd iteration is 0.607100The answer is repeated at 3 iteration is 0.607100
-
8/2/2019 NACP Programs (1)
42/79
23. Newton Gregory forward Interpolation method.
/*NEWTON GREGORY FORWARD INTERPOLATION*/
#include
#include
#include
void
main()
{
int n,i,j,k;
float mx[10],my[10],x,y=0,h,p,diff[20]
[20],y1,y2,y3,y4; clrscr();
printf("\nenter no. of
terms:"); scanf("%d",&n);
printf("\nenter values
x\ty:\n"); for(i=0;i
-
8/2/2019 NACP Programs (1)
43/79
OUTPUT:
Enter no. of terms:5
Enter values x y:
3 135 2311 89927 1731534 35606
Enter value of x at which y is to be calculated:7
When
x=7.0000,y=899.00000000
-
8/2/2019 NACP Programs (1)
44/79
24. Newton Gregory backward Interpolation method.
/*NEWTON GREGORY BACKWARD INTERPOLATION*/
#include
#include
#include
void
main(){
int n,i,j,k;
float mx[10],my[10],x,x0=0,y0,sum=0,fun=1,h,p,diff[20][20],y1,y2,y3,y4;
clrscr();
printf("\nenter no. of
terms:"); scanf("%d",&n);
printf("\nenter valuesx\ty:\n"); for(i=0;i
-
8/2/2019 NACP Programs (1)
45/79
1)))/k;
sum=sum+fun*diff[i]
[k]; }
printf("\nwhen x=%6.4f,y=
%6.8f",x,sum); getch();
}
OUTPUT:
Enter no. of terms:5
Enter values x y:20 4140 103
60 16880 218100 235
Enter value of x at which y is to be calculated:70
When x=70.0000,y=196.00000000
-
8/2/2019 NACP Programs (1)
46/79
25. Lagranges method of Interpolation.
/*LAGRANGE METHOD OF INTERPOLATION*/
#include
#include
#include
#define
max 50
void
main()
{
float
ax[max],ay[max],nr,dr,x,y=0
; int i,j,n;
clrscr();
printf("\nEnter No. of
Points:"); scanf("%d",&n);printf("\nEnter the given set of
values:\nx\ty\n"); for(i=0;i
-
8/2/2019 NACP Programs (1)
47/79
10 90011 121013 2028
Enter the value of x at which f(x)is required:8 when x= 8.00 theny=445.62
-
8/2/2019 NACP Programs (1)
48/79
26. Write a program in C/C++ which can calculate the value of afunction at a point using Newton Divided Difference method.
/* NEWTON DIVIDED DIFFERENCE METHOD */
#include
#include
#include
void
main(){
float ax[20], ay[20],
diff[30],temp=1; int
n,j,m,z=0,A=0,k=0;clrscr();
cout>n;
for (int
i=0;i
-
8/2/2019 NACP Programs (1)
49/79
1
;
A
=
0
;
for(j=0;j
-
8/2/2019 NACP Programs (1)
50/79
A=0;
for
(z=0;z
-
8/2/2019 NACP Programs (1)
51/79
9
2432
42
11
0The value of y for x = 9 is: 810
-
8/2/2019 NACP Programs (1)
52/79
27. Write a program in C for Bessels Method of Interpolation.
/*BESSEL'S METHOD OF INTERPOLATION*/
#include
#include
#include
void
main()
{ int n , i , j ;
float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 , y3
, y4 ; clrscr();
printf("\nEnter the noumber of
item : "); scanf("%d" , &n);
printf("\nEnter the value in the form of
x\n"); for(i = 0 ; i < n ; i++)
{ printf("\nEnter the value of x%d\t" , i+1);
scanf("%f" , &ax[i]); }printf("\nEnter the value in the form of y\n");
for(i = 0 ; i < n ; i++)
{ printf("\nEnter the value of y%d\t" , i+1);
scanf("%f" , &ay[i]); }
printf("\nEnter the value of x for which you want the value of y
:- "); scanf("%f" , &x);
h = ax[1] - ax[0];
for(i = 0 ; i < n-1 ; i++)
diff[i][1] = ay[i+1] -
ay[i]; for(j = 2 ; j
-
8/2/2019 NACP Programs (1)
53/79
OUTPUT
Enter number of item: 4
Enter the value in the form of x
Enter the value of x1 20Enter the value of x2 24Enter the value of x3 28Enter the value of x4 32
Enter the value in the form of y
Enter the value of y1 24Enter the value of y2 32Enter the value of y3 35Enter the value of y4 40
Enter the value of x for which you want the value of y:25
When x = 25.0000 , y = 32.945313
-
8/2/2019 NACP Programs (1)
54/79
28. Write a program for Stirlings Method of Interpolation.
/*STIRLING METHOD OF INTERPOLATION*/
#include#include
#include
void
main()
{ int n , i , j ;
float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] ,
y1 , y2 , y3 ,y4;
clrscr();
printf("\nEnter the noumber of
item : "); scanf("%d" , &n);
printf("\nEnter the value in the form ofx\n"); for(i = 0 ; i < n ; i++)
{printf("\nEnter the value of x
%d\t" , i+1); scanf("%f" , &ax[i]); }
printf("\nEnter the value in the form of
y\n"); for(i = 0 ; i < n ; i++)
{printf("\nEnter the value of y
%d\t" , i+1); scanf("%f" , &ay[i]); }
printf("\nEnter the value of x for which you want the value of y
:- ");
scanf("%f"
, &x);
h = ax[1] -
ax[0];
for(i = 0 ; i < n-1 ; i++)diff[i][1] = ay[i+1] -
ay[i]; for(j = 2 ; j
-
8/2/2019 NACP Programs (1)
55/79
OUTPUT
Enter the number of item : 4
Enter the value in the form of xEnter the value of x1 20Enter the value of x2 24Enter the value of x3 28Enter the value of x4 32
Enter the value in the form of y
Enter the value of y124Enter the value of y2 32
Enter the value of y3 35Enter the value of y4 40
Enter the value of x for which you want the value of y:25
When x = 25.0000, y = 33.31251144
-
8/2/2019 NACP Programs (1)
56/79
29. Write a program for trapezoidal rule for Numerical Integration.
/* TRAPEZOIDAL RULE */
#include
#include
#include
void main()
{
float
fun(float);
float h ,
k1=0.0 ;
float x[20] ,
y[20];
int n , i;
clrscr();
printf("\nEnter number of
parts : "); scanf("%d" , &n);
printf("\nEnter lower and upper
limits : "); scanf("%f %f" , &x[0] ,
&x[n]);
y[0] = fun(x[0]);
h = (x[n] - x[0])/n ;
printf("\nx y");
printf("\n %8.5f %8.5f " , x[0] ,y[0]);
for(i=1 ; i < n ; i++)
{ x[i] = x[0] + i * h ;y[i] = fun(x[i]);
printf("\n %8.5f %8.5f " , x[i] , y[i]);
k1 = k1 + 2 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] ,
y[n]);
y[0] = (h / 2.0 ) * (y[0] + y[n]
+ k1 );
printf("\nresult = %f \n" ,
y[0]);
getch();
}
float
fun(float
x)
{
float g;
g = log(x);
return g;
}
-
8/2/2019 NACP Programs (1)
57/79
OUTPUT:
Enter number of parts: 6Lower and upper limits: 4 5.2
x y4.00000 1.386294.24000 1.444564.48000 1.499624.72000 1.551814.96000 1.601415.20000 1.64866
Result = 1.827570
-
8/2/2019 NACP Programs (1)
58/79
30. Write a program for Simpsons One Third Rule.
/* SIMPSION 1/3 RULE */
#include#include
#include
void
main()
{ float fun(float);
float h , k1=0.0 ,
k2=0.0 ; float x[20]
, y[20];
int n , i;
clrscr();
printf("\nEnter number of
parts : "); scanf("%d" , &n);printf("\nEnter lower and upper
limits :"); scanf("%f %f" , &x[0] ,
&x[n]);
y[0] = fun(x[0]);
h = (x[n] - x[0])/n ;
printf("\nx y");
printf("\n%8.5f %8.5f" , x[0] ,y[0]);
for(i=1 ; i < n ; i++)
{ x[i] = x[0] + i * h ;y[i] = fun(x[i]);
printf("\n %8.5f %8.5f " , x[i] , y[i]);
if(i % 2 == 0)
k1 = k1 + 2 * y[i];else
k2 = k2 + 4 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = (h / 3.0 ) * (y[0] + y[n] + k1
+ k2 ); printf("\nresult =%f \n" ,
y[0]);getch();
}
float fun(float x)
{ float g;
g = sin(x) - log(x) +
exp(x); return g;}
OUTPUT :Enter number of parts : 6Enter lower and upper limits :0.2 1.4x y
-
8/2/2019 NACP Programs (1)
59/79
0.20000 3.029510.40000 2.797530.60000 2.897590.80000 3.166041.00000 3.559751.20000 4.069831.40000 4.70418Result = 4.052133
-
8/2/2019 NACP Programs (1)
60/79
31. Simpsons 3/8 rule.
/*SIMPSION 3/8 RULE */
#include
#include
#include
void
main()
{ float fun(float);
float h , k1=0.0 ,
k2=0.0 ; float x[20]
, y[20];
int n , i;
clrscr();
printf("\nEnter number of
parts : "); scanf("%d" , &n);printf("\nEnter lower and upper
limits : "); scanf("%f %f" , &x[0] ,
&x[n]);
y[0] = fun(x[0]);
h = (x[n] - x[0])/n ;
printf("\nx y");
printf("\n%8.5f %8.5f" , x[0] ,y[0]);
for(i=1 ; i < n ; i++)
{ x[i] = x[0] + i * h ;
y[i] = fun(x[i]);
printf("\n %8.5f %8.5f " , x[i] , y[i]);
if(i % 3 == 0)
k1 = k1 + 2 * y[i];
elsek2 = k2 + 3 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = ((3 *h) / 8.0 ) * (y[0] + y[n] + k1
+ k2 ); printf("\nresult =%f \n" , y[0]);getch();
}
float fun(float x)
{ float g;
g = sin(x) - log(x) +
exp(x); return g;
}
-
8/2/2019 NACP Programs (1)
61/79
OUTPUT :
Enter number of part parts : 6Enter lower and upper limits : 0.2 1.4
x y
0.20000 3.029510.40000 2.797530.60000 2.897590.80000 3.166041.00000 3.559751.20000 4.069831.40000 4.70418Result = 4.052991
-
8/2/2019 NACP Programs (1)
62/79
32.Booles Rule for Numerical Integration.
/* BOOLS RULE */
#include
#include
#include
void
main()
{ float fun(float);
float h , k1=0.0 , k2=0.0 , k3=0.0 ,
k4=0.0; float x[20] , y[20];int n , i;
clrscr();printf("\nEnter number of
parts"); scanf("%d" , &n);
printf("\nEnter lower and upper
limits :"); scanf("%f %f" , &x[0] ,
&x[n]);y[0] = fun(x[0]);
h = (x[n] - x[0]) / n;
printf("\nx y");
printf("\n%8.5f %8.5f" , x[0] ,y[0]);for(i=1 ; i < n ; i++)
{ x[i] = x[0] + i * h ;
y[i] = fun(x[i]);
printf("\n %8.5f %8.5f " , x[i] , y[i]);
if(i % 2 == 0)
k1 = k1 + 12 * y[i];
else
k1 = k1 + 32 * y[i];
}y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = ((2 * h)/45) * (7 * y[0] + 7 * y[n] + k1 + k2 + k3
+ k4); printf("\nresult =%f \n" , y[0]);
getch();
}
float fun(float x)
{ float g;g = log(x);
return g;
}
OUTPUT :Enter number of parts : 6Enter lower and upper limits : 4 5.2
x y4.00000 1.38629
-
8/2/2019 NACP Programs (1)
63/79
-
8/2/2019 NACP Programs (1)
64/79
-
8/2/2019 NACP Programs (1)
65/79
OUTPUT :Enter number of parts : 6
Enter lower and upper limits : 0.2 1.4x y0.20000 3.029510.40000 2.797530.60000 2.897590.80000 3.166041.00000 3.55975
1.20000 4.069831.40000 4.70418Result = 4.051446
-
8/2/2019 NACP Programs (1)
66/79
34. Write a program for Gauss Elimination Method in C.
/* GAUSS ELIMINATION METHOD */
#include
#include
#include
#define n
3void main()
{ float temp , s , matrix[n][n+1] , x[n];
int i , j , k;
clrscr();
printf("\nEnter the elements of the augment matrix row wise
:\n");for(i = 0 ; i < n ; i++)
for(j=0 ; j
-
8/2/2019 NACP Programs (1)
67/79
OUTPUT:
Enter the elements of the augment matrix row wise :3 1 -1 32 -8 1 -51 -2 9 8
Matrix :3.000000 1.000000 -1.000000 3.0000002.000000 -8.000000 1.000000 -5.0000001.000000 -2.000000 9.000000 8.000000
Solution is :
x[1]= 1.0000x[2]= 1.0000x[3]= 1.0000
-
8/2/2019 NACP Programs (1)
68/79
35. Write a program for Gauss Jordon Method in C.
/* GAUSS JORDAN METHOD */
#include
#include
#include
#define n
3
void main()
{ float temp , matrix[n][n+1];
int i , j , k;
clrscr();
printf("\nEnter the elements of the augment matrix row wise :-
\n");
for(i = 0 ; i < n ; i++)
for(j=0 ; j
-
8/2/2019 NACP Programs (1)
69/79
1 -2 9 8The digonal matrix is :--3.000000 0.000000
0.000000 -8.6666670.000000
0.000000 Solution is :-x[1]= 1.0000x[2]= 1.0000x[3]= 1.0000
0.000000 3.0000000.000000 -8.6666678.884615 8.884615
-
8/2/2019 NACP Programs (1)
70/79
36. Write a program for Gauss Seidal Method in C.
/* GAUSS SEIDAL METHOD */
#include
#include#include
#include
void main()
{
float a[10][10],x[10],aerr, maxerr, t,
s, err; int i,j,itr,maxitr,n;
printf ("\n Enter the number of
unknowns="); scanf ("%d",&n);
for(i=1;i