Professional Documents
Culture Documents
Program no.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<stdio.h>
# include<conio.h>
void main( )
{
int i, n=100,sum=0,p;
float avg;
for (i=1;i<=n;i++)
{
p=i*i ;
sum=sum+p;
}
avg =sum/(i-1);
printf ("\n The sum is %d", sum);
printf ("\n The average is %f", avg);
OUTPUT:
The sum is 10670
The average is 106.000000
Program no.2
/*PROGRAM TO FIND PRODUCT OF 1ST 'N' NATURAL NUMBERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int prod;
clrscr();
5
Product of 1st 5 natural numbers: 1203
Program no 3.
/* FIBBONACCI SERIES */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i;
double
x1,x2,x; x1=1;
x2=1;
printf("%12.0f\t%12.0f",x1,x2);
for(i=3;i<=25;i++)
{
x=x1+x2;
x1=x2;
x2=x;
printf("%12.0f",x);
}
getch();
}
OUTPUT:
1
13
21
Program no.4
/*GENERATING PRIME NO. BETWEEN 1 AND 100*/
#include<stdio.h>
#include<conio.h>
void main()
int i,j,count=0;
clrscr();
printf("\nprime nos. between
100\n"); for(i=1;i<=100;i++)
{ if(i==2) i+
+; j=2;
while(j<=(i/2))
{ if(i%j==0)
break; j+
+;
}
if(j>(i/2))
{ count++; printf("\t
%d",i);
}
if(count%5==0)
printf("\n");
}
getch();
}
output:
and
7
23
43
67
89
11
29
47
71
97
5
Program no.5
Write a program in C to generate prime nos. between 1 and 100 except
those divisible by 5.
/* GENERATE PRIME NOS. BETWEEN 1 AND 100 EXCEPT THOSE DIVISIBLE
BY 5*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n1=1,n2=100,j,i,temp,k=0,flag=0;
for (i=n1;i<=n2;i++)
{
temp=i;
if (temp==2)
{
flag=1;
}
if (temp%5==0)
{
flag=0;
continue;
}
for (j=2;j<=temp/2;j++)
{
if (temp%j!=0)
{
flag=1;
}
else
{
flag=0;
break;
}
}
if (flag==1)
{
printf ("\t %d",temp);
}
}
OUTPU
T:
31
73
2
37
79
3
41
83
7
43
89
11
47
97
13
53
17
59
19
61
23
67
29
71
6
Program no. 6
/*CALCULATION OF THE VALUE OF nCr*/
#include<stdio.h>
#include<conio.h>
void main()
int n,r,y;
clrscr();
printf("\nCALCULATION OF nCr");
printf("\nenter the value of
n ,r"); scanf("%d%d",&n,&r);
y=fact(n)/(fact(r)*fact(n-r));
printf("\n%dC%d = %d",n,r,y);
getch();
}
int fact (int a)
{
int f=1,j;
if((a==0)&&(a==1))
return(1);
else
{
for(j=1;j<=a;j++)
f=f*j;
return(f);
}
}
OUTPUT:
CALCULATION OF nCr
enter the value of n ,r6
2 6C2 = 15
Program no. 7
/*SUM OF DIGITS OF A GIVEN NUMBER*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num,p,sum=0;
clrscr();
printf("\nenter any
number=="); scanf("%d",&num);
while(num!=0)
{
p=num%10;
sum=sum+p;
num=num/10;
}
printf("\nsum of digit of given
number=="); printf("%d",sum);
number=="); printf("%d",sum);
getch();
}
OUTPUT:
enter any number==4672
sum of digit given number==19
8
Program no. 8
Write a program in C to convert binary number to decimal
number. /* CONVERT BINARY NUMBER TO DECIMAL
NUMBER */
#include<stdio.h>
#include<conio.h>
#incl
ude<m
ath.h
>
void
main(
)
{
int num[10];
int i,x=2,j,y=0,n;
printf ("\n Enter the number of elements of
binary number="); scanf ("%d",&n);
j=n-1;
for (i=0;i<n;i++)
{
printf ("\n Enter the
number[%d]=",i); scanf
("%d",&num[i]);
}
while (j>=0)
{
for (i=0;i<n;i++)
{
y=y
+num[i]*(po
w(x,j));
j--;
}
}
printf (" The decimal number is %d",y);
}
OUTPUT:
Enter the number of elements of binary number= 5
Enter the number[0]= 1
Enter the number[1]= 0
Enter the number[2]= 1
Enter the number[3]= 1
Enter the number[4]= 1
The decimal number is 23
9
Program no.9
Write a program in C to convert decimal number to binary
number. /* CONVERT DECIMAL NUMBER TO BINARY
NUMBER */
#include<stdio.h>
#inclu
de<con
io.h>
void
main()
{
i
n
t
i
=
0
,
j
,
n
;
i
n
t
r
e
m
[
1
0
]
;
OUTPUT:
Enter the number= 123
The binary number is 1111011
10
Program no.10
Write a program in C to add two rectangular
matrices. /* ADDTION OF MATRIX */
#include<stdio.h>
#include<co
nio.h> void
main()
nio.h> 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<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The matrix 1 is [%d]
[%d]=",i,j); scanf ("%d",& mat1[i]
[j]);
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The matrix 2is [%d]
[%d]=",i,j); scanf ("%d",& mat2[i]
[j]);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
mat3[i][j]= mat1[i][j]+mat2[i][j];
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The resultant matrix is %d\n",mat3[i]
[j]);
}
printf ("\n");
}
OUTPUT:
The number of rows are=2
The number of columns are=2
11
The
The
The
The
The
The
The
The
matrix
matrix
matrix
matrix
matrix
matrix
matrix
matrix
1
1
1
1
2
2
2
2
is
is
is
is
is
is
is
is
[0][0]=
[0][1]=
[1][0]=
[1][1]=
[0][0]=
[0][1]=
[1][0]=
[1][1]=
1
2
3
4
5
6
7
8
8
1
2
10
12
Program no. 11
/*MULTIPLICATION OF MATRIX*/
#include<stdio.h>
#inclu
de<con
io.h>
void
main()
{int m,n,p,q,i,j,k;
int a[10][10],b[10]
[10],c[10][10];
clrscr();
}
printf("\nenter
matrix b\n");
for(i=0;i<p;++i)
{ for(j=0;j<q
;j++)
scanf("%d"
,&b[i]
[j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ c[i]
[j]=0
;
for(k
=0;k<
n;k+
+)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("the product
ofmatrix is:");
for(i=0;i<m;i++)
{printf("\n");
for(j=0;j<q;j++)
printf("%3d",c[i][j]);
}
end:
getch();
}
13
Output:
2 -1
The product of matrix
is: 3 -2 5 -5 7 -8
14
Program no.12
/*BISECTION METHOD*/
#include<stdio.h>
#include<conio.h>
#incl
ude<m
ath.h
>
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(x1)
f(x2)
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
%f",i,x1,x2,x3,p,q,r);
if((p*r)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))<=0.001)
{printf("\n
root of the equ is %f",x3);
getch();
exit(0);
}
els
e
goto l15;
}
float fun(float m)
{float g;
float fun(float m)
{float g;
g=(m*(exp(m))-1);
return(g);
}
/*output:
equation:x*(exp(x)-1)
enter the app
value of x1,x2:0.5 1
th
e
root lies b/w 0.500000 & 1.000000
x
n
x1
2
x3
1
0.500000
1.000000
0.750000
2
0.500000
0.750000
0.625000
3
0.500000
0.625000
0.562500
4
0.562500
0.625000
0.593750
5
0.562500
0.593750
0.578125
6
0.562500
0.578125
0.570312
7
0.562500
0.570312
0.566406
8
0.566406
0.570312
0.568359
9
0.566406
0.568359
0.567383
10
0.566406
root of the
equ
0.567383
is
0.566895
0.566895
f(x1)
-0.175639
-0.175639
-0.175639
-0.012782
-0.012782
-0.012782
-0.012782
-0.002035
-0.002035
-0.002035
f(x2)
1.718282
0.587750
0.167654
0.167654
0.075142
0.030619
0.008780
0.008780
0.003364
0.000662
f(x3)
0.587750
0.167654
-0.012782
0.075142
0.030619
0.008780
-0.002035
0.003364
0.000662
-0.00068
7
15
Program no. 13
/*NEWTON RALPHSON*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
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
if (fabs (df(x0))<delta)
{printf("slope is very small and= %f",df(x0));}
else a10: p=f(x0);q=df(x0);
x1=x0-(p/q);
i++;
printf("\n %d\t%f\t%f\t%f\t%f\n",i,x0,x1,p,q);
if(fabs((x1-x0)/x1)<=error)
{printf("\nThe root of equation X^3+1.2X^2-5X-7.2 is %f",x0);
getch();
exit(0);}
else
{x0=x1;
goto a10;}
}
/* Function sub program */
float f(float a)
{float g;
g = pow(a,3)+((1.2)*(pow(a,2)))-5*a-7.2;
return(g);}
float df(float a)
{float g1;
f(x0)
df(x0)\n
");
{float g1;
g1 = (3*pow(a,2))+(2.4*a)-5;
return(g1);
}
/
*
output:
The equation is X^3+1.2X^2-5X-7.2
enter the initial value of x0:2
i
x0
x1
f(x0)
2
2.000000
2.372881
-4.400000
3
2.372881
2.313010
1.052938
4
2.313010
2.311227
0.029603
5
2.311227
2.311225
0.000026
The root of equation X^3+1.2X^2-5X-7.2 is 2.311227
df(x0)
11.800000
17.586615
16.601265
16.572248
16
Program no.14
/*REGULA-FALSE METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float
f(float x)
{return(3*x
-cos(x)-1);
}
void main()
{ float
f(flo
at
x);
doubl
e
x1,x2
,m;
int
c=0;
clrsc
r();
printf("\n enter the first
approximation
:");
scanf("%f",&x1);
printf("\n enter the second
approximation
:");
scanf("%f",&x2);
if(f(x1)*f(x2)<0.0)
{
m=(x1*f(x2)-x2*f(x1))/
( f ( x 2 ) - f ( x 1 ) ) ;
while(fabs(f(m))>=0.0001
)
{
c++;
if(f(x1)*f(m)<0.0)
x
2
=
m
;
e
l
s
l
s
e
x
1
=
m
;
m=((x1*f(x2))-(x2*f(x1)))/(f(x2)f(x1)); printf("\n\n the
%d,ilteration is %f ",c,m);
}
printf("\n\n the answer is
%f",c,m);
}
else
printf("enter valid initial
approximation :"); getch();
}
repeated
at
%d
ilteration
is
OUTPUT:
Enter the first approximation: 0
Enter the second approximation: 1
The 1, iteration is 0.605959
The 2, iteration is 0.607057
The 3, iteration is 0.607100
The answer is repeated at 3 ilteration is 0.607100
17
Program no.15
{
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<n;i++)
scanf("%f
%f",&mx[i],&my[i]);
printf("\nenter value of x at which y is to be
calculated:"); scanf("%f",&x);
h=mx[1]mx[0];
for(i=0;i<n-1
;i++) diff[i]
[1]=my[i+1]my[i];
for(j=2;j<=4;
j++)
for(i=0;i<n;i
j++)
for(i=0;i<n;i
++)
diff[i][j]=diff[i+1][j-1]diff[i][j-1]; i=0;
do {i++;
}
while(m
x[i]<x)
; i--;
p=(xmx[i])/
h;
y1=p*di
ff[i-1]
[1];
y2=p*(p+1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2]
[3]/6; y4=(p+2)*(p
+1)*p*(p-1)*diff[i-3][4]/
24; y=my[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f,y=
%6.8f",x,y); getch();
}
OUTPUT:
Enter no. of terms: 5
Enter values x y:
3
13
5
23
11
899
1731
27
5
3560
34
6
Enter value of x at which y is to be calculated: 7
When x=7.0000,y=899.00000000
Program no.16
/*NEWTON GREGORY BACKWARD INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
#incl
ude<m
ath.h
>
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
values x\ty:\n");
18
19
printf("\nenter
values x\ty:\n");
for(i=0;i<n;i++)
scanf("%f%f",&mx[i],&my[i]);
printf("\nenter value of x at which y is to be
calculated:"); scanf("%f",&x);
h=mx[1]
-mx[0];
for(i=0
;i<n-1;
i++)
diff[i][1]=my[i+1]-my[i];
for(j=2;j<=4;j++)
for(i=0;i<n;i++) diff[i]
[j]=diff[i+1][j-1]-diff[i]
[j-1];
i=0;
while(!mx[i]>x)
i++;
x0=mx[i];
y
0
=
m
y
[
i
]
;
p
=
(
x
x
0
)
/
h
;
s
u
m
=
y
0
;
for(k=1;k<=4;k++)
{
fun=(fun*(p(k-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
41
10
40
3
16
60
8
21
8
23
5
80
100
20
21
Program no.17
/*LAGRANGE METHOD OF INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
#incl
ude<m
ath.h
>
#defi
ne
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<n;i++)
scanf("%f%f",&ax[i],&ay[i]);
printf("\nEnter the value of x at which f(x)is
required:"); scanf("%f",&x);
for(i=0;i<n;i++)
{
nr=dr=1;
for(j=0;j<n;j++)
if(j!=i)
{
nr*=x
ax[j]
;
dr*=a
x[i]ax[j]
;
}
y+=(nr/dr)*ay[i];
}
printf("\nwhen x=%5.2f then y=
%5.2f",x,y); getch();
}
Output:
5
7
10
11
13
100
294
900
1210
2028
22
Program no.18
Write a program in C/C++ which can calculate the value of a function at a
point using Newton Divided Difference method.
/* NEWTON DIVIDED DIFFERENCE METHOD */
#include<iostream.h>
#include<conio.h>
#incl
ude<m
ath.h
>
void
main(
)
{
float ax[20], ay[20],
diff[30],temp=1; int
n,j,m,z=0,A=0,k=0;
clrscr();
cout<<"Enter the number
of points="; cin>>n;
for (int i=0;i<n;i++)
{
cout<<"Enter ( x"<<i+1<<" ,y"<<i
+1<<" )\n"; cin>>ax[i]>>ay[i];
}
cout<<"Enter the
value of x=";
cin>>ax[n];
// creating difference table
for (i=0;i<n-1;i++)
{
diff[i]= (ay[i+1]-ay[i])/(ax[i+1]-ax[i]);
}
if(n>1)
{
m=n-1; A=0;
for(j=0;j<n-2;j++)
{
for(z=0;z<m-1;i++,z++)
{
diff[i]= (diff[z+1+A]-diff[z+A])/(ax[z+j+2]-ax[z]);
}
A+=m; m--;
}
}
//printing difference table
cout<<"\n difference table is as
follows:\n"; for(z=0;z<i;z++)
23
A=0;
for (z=0;z<n-1;z++)
{
temp*=(ax[n]-ax[z]);
ay[n]
+=temp*diff
[z+A]; A
+=m-2;
m--;
}
cout<<"\n\n The value of y for x = "<<ax[n]<<"
is :"<<ay[n]; getch();
}
OUTPUT:
Enter the number
of points=5 Enter
(x1, y1) 5 150
Enter (x2,
y2) 7 392
Enter (x3,
y3) 11 1452
Enter (x4,
y4) 13 2366
Enter (x5,
y5) 17 5202
Enter the
value of x=9
Program no.19
24
ath.h
>
void
main(
)
{ int n , i , j ;
25
OUTPUT:
Enter number of item: 4
Enter the value in the form of x
Enter the value of x1
Enter the value of x2
Enter
the
value
of
Enter
the
value
of
20
24
x3
x4
28
32
24
32
y3
y4
35
40
26
Program no 20
/******STIRLING METHOD OF INTERPOLATION******/
#include<stdio.h>
#include<conio.h>
#include<math.h>
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]); }
the value of x for which you want the value of
printf("\nEnter
y
:- ");
scanf("%f" , &x);
h = ax[1] - ax[0];
n-1 ; i+
for(i = 0 ; i <
+)
diff[i][1] = ay[i+1] - ay[i];
for(j = 2 ; j <= n ; j++)
for(i = 0
; i < -j ; i++)
diff[i][j] = diff[i+1][j-1] - diff[i][j-1];
i=0;
do
{ i++;
} while(ax[i] < x);
i--;
p = (x - ax[i])
/ h;
y1 = p * (diff[i][1] + diff[i - 1][1]) / 2;
y2 = p * p * (diff[i - 1][2]) / 2;
- 1) * (diff[i - 1 ][3] + diff[i - 2][3]) /
y3 = p * (p * p
12 ;
y4 = p * p * (p
* p - 1) * diff[i-2][4] / 24;
y = ay[i] + y1 +y2 + y3 + y4;
printf("\nWhen x = %6.4f , y = %6.8f" , x ,
y); getch();
OUTPUT:
Enter the number of item: 4
Enter the value in the form of x
Enter the value of x1
Enter the value of x2
Enter the value of x3
Enter
the
value
of
Enter
the
value
in
20
24
28
x4
the form of
y
32
27
the
value
of
24
32
35
y4
x for which you want the value of y :25
40
28
Program no.21
/*
TRAPEZOIDAL RULE */
#include<stdio.h>
#include<math.h>
#include<conio.h>
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");
%8.5f " ,
printf("\n %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;
OUTPUT:
Enter number of parts: 6 lower
and upper limits: 4 5.2
4.00000
4.24000
4.48000
4.72000
4.96000
1.38629
1.44456
1.49962
1.55181
1.60141
29
5.20000 1.64866 result
= 1.827570
30
Program no.22
/* SIMPSION 1/3 RULE */
#include<stdio.h>
#include<math.h>
#include<conio.h>
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
"); scanf("%d" , &n);
of
parts
y[0] = fun(x[0]);
h = (x[n]
printf("\nx
x[0])/n ;
y");
%8.5f" ,
printf("\n%8.5f
x[0] ,y[0]);
for(i=1 ;
i < n
; i++)
{ x[i] = x[0] + i * h ;
y[i] = fun(x[i]);
%8.5f " , x[i] ,
printf("\n
%8.5f
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;
}
31
Program no.23
/*SIMPSION 3/8 RULE */
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{ float fun(float);
float h , k1=0.0
k2=0.0 ; float x[20]
y[20];
,
,
int n , i;
clrscr();
y
3.02951
0.20000
3.02951
0.40000
2.79753
0.60000
2.89759
0.80000
3.16604
1.00000
3.55975
1.20000
4.06983
1.40000
4.70418
Result = 4.052991
32
Program no.24
/* BOOLES RULE */
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{ float fun(float);
k3=0.0
int n , i;
clrscr();
printf("\nEnter number
parts"); scanf("%d" , &n);
of
y[0] = fun(x[0]);
h = (x[n]
x[0]) / n;
printf("\nx
y");
%8.5f" ,
printf("\n%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;
y
1.38629
1.43508
1.48160
1.52606
1.56862
4.80000
1.56862
5.00000
1.60944
5.20000
1.64866
Result = 1.81427
33
Program no.25
/* WEDDLE'S RULE */
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{ float fun(float);
k3=0.0
int n , i;
clrscr();
else
k1 +
3 ==
k1 +
2 ==
2 * y[i];
0)
6 * y[i];
0)
k1 = k1 + y[i];
k4 = k4 + 5 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = ((3 * h)/10) * (y[0] + y[n] + k1 + k2 + k3 +
k4); printf("\nresult =%f \n" , y[0]);
getch();
}
float fun(float x)
{ float g;
g = sin(x) - log(x)
exp(x); return g;
}
y
3.02951
2.79753
2.89759
3.16604
3.55975
4.06983
1.00000
1.20000
1.40000
result
3.55975
4.06983
4.70418
= 4.051446
Program no.26
/* GAUSS ELIMINATION METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 3
void 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 <= n ; j++)
scanf("%f" , &matrix[i][j]);
printf("\nmatrix:-");
for(i=0 ; i<n ; i++)
{ for(j=0 ;j<=n ;j++) printf("%f
\t",matrix[i][j]);
printf("\n");
}
/*now calculating the upper triangular matrix
*/ for(j=0 ; j < n-1 ; j++)
for(i=j+1 ; i < n ; i++)
{ temp = matrix[i][j] / matrix[j][j];
for(k = 0 ; k <= n ; k++) matrix[i]
[k] -= matrix[j][k] * temp;
}
//now performing back substitution
for(i = n -1 ; i >= 0 ; i--)
{ s = 0;
printf("\nx[%d]=%7.4f" , i
+1 ,x[i]); getch();
}
34
x[2]= 1.0000
x[3]= 1.0000
35
Program no.27
/* GAUSS JORDAN METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#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 <= n ; j++)
scanf("%f" , &matrix[i]
[j]); /*now calculating the digonal matrix
*/ for(j=0 ; j < n ; j++)
for(i=0 ; i < n ; i+
+) if(j != i)
{ temp = matrix[i][j] / matrix[j][j];
for(k = 0 ; k <= n ; k++) matrix[i]
[k] -= matrix[j][k] * temp;
}
/*now printing the matrix */
printf("\nThe digonal matrix is :-\n"); for(i = 0 ; i < n ; i++)
{ for(j=0 ; j <= n ; j++)
printf("\t%f" , matrix[i]
[j]); printf("\n");
}
//now printing the result
printf("\nSolution is :-\n");
for(i = 0 ; i < n ; i++)
3.000000
-8.666667
8.884615
36
Program no.28
Program no.28
/* GAUSS SEIDAL METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
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<=n;i++)
x[i]=0.0;
printf ("\n Enter the augmented matrix row
wise="); for (i=1;i<=n;i++)
for (j=1;j<=n+1;j++)
scanf ("%f",&a[i][j]);
for (itr=1;itr<=maxitr;i++)
{
maxerr=0;
for (i=1;i<=n;i++)
{
s=0;
for (j=1;j<=n;j++)
if (j!=i)
s = s+a[i][j]*x[j];
t = (a[i][n+1]-s)/a[i][i];
err=fabs(x[i]-t);
if (err>maxerr)
maxerr=err;
x[i]=t;
}
printf ("%d",itr); for
(i=1;i<=n;i++) printf
("%7.4f",x[i]);
if (maxerr<aerr)
{
for (i=1;i<=n;i++)
} }
printf ("x[%d]=%7.4f",i,x[i]);
exit(0);
OUTPUT:
Enter the number of unknowns = 3
Enter the augmented matrix row
wise 20 1 -2 17 3 20 -1 -18 2 -3
20 25
Enter allowed error, max iteration=0.001 ,4
Iteration x[1]= 0.8500 -1.0275
x[2] = 1.01091 1.0025
x[3] = -0.9998 0.99981
1.0000 - 1.0000 1.0000
37
Program no.29
/* CURVE FITTING - STRAIGHT LINE */
# include<iostream.h>
# include<conio.h>
#
include<math.h>
void main()
{
int i=0,ob;
float x[10],y[10],xy[10],x2[10],sum1=0,sum2=0,sum3=0,sum4=0;
double a,b;
printf("\n
Enter
the
no.
of
observations :"); scanf(%d,&ob);
printf("\n Enter the values of x :
\n"); for (i=0;i<ob;i++)
{
Printf("\n Enter the value of x[%d]",i+1);
Scanf(%f,&x[i]);
sum1+=x[i];
}
Printf(\n Enter the values of y :
\n"); for (i=0;i<ob;i++)
{
Printf("\n Enter the value of y[%d]",i+1);
Scanf(%f,&y[i];
sum2+=y[i];
}
for(i=0;i<ob;i++)
{
xy[i]=x[i]*y[i];
sum3+=xy[i];
}
for(i=0;i<ob;i++)
{
x2[i]=x[i]*x[i];
sum4+=x2[i];
}
a=(sum2*sum4-sum3*sum1)/(ob*sum4sum1*sum1); b=(sum2-ob*a)/sum1;
printf("\n\n Equation of the STRAIGHT LINE of the form y=a+b*x is
); printf("\n\n\t\t\t y=[%f] + [%f]x.a,b);
}
OUTPUT:
Enter
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
the
no. of observations : 5
values of x :
value of x1 : 1
value of x2 : 2
value of x3 : 3
value of x4 : 4
value of x5 : 5
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
values of y :
value of y1: 14
value of y2: 27
value of y3: 40
value of y4: 55
value of y5: 68
38
Program no.30
getch();
39
OUTPUT :dy/dx
(x^3 +y^2)/10
given y0 = 1
The solution
1.053870
The solution
1.116833
The solution
1.194833
The solution
1.297048
The solution
1.436797
The solution
1.633150
The solution
1.914011
The solution
2.322503
The solution
2.931453
The solution
x0 < x <
0.100000
2.1
)
y =
0.300000
y =
0.500000
y =
0.700000
y =
0.900000
y =
1.100000
y =
1.300000
y =
1.500000
y =
1.700000
y =
1.900000
y =
2.931453
The solution of differential equation is when x =
3.880822
The solution of differential equation is when x =
5.495997
1.900000
y =
2.100000
y =