Beruflich Dokumente
Kultur Dokumente
Programming
Programs
Submitted By:
Mohd.Yawar Nihal Siddiqui
10-CSS-40
B.Tech, Computer Engineering
IVth Semester
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 DIGITS
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...........................................................
OUTPUT:
The sum is 10670
The average is 106.000000
Output:
Enter the value of n: 5
Product of 1st 5 natural numbers: 120
OUTPUT:
Principal
amount p
=1000
Rate of
interest r =10
Time period t
=5
Simple Interest =
500.000000
Compound
Interest =
610.51013
OUTPUT:
1
13
21
#include<stdio.h>
#include<c
onio.h>
void
main()
{
int i,j,count=0;
clrscr();
printf("\nprime nos. between 1 and
100\n"); for(i=1;i<=100;i++)
{
if(i==2)
i
+
+
;
j
=
2
;
while(j<=(i/2))
{
if(i%j==0)
b
r
e
a
k
;
j
+
+
;
}
if(j>(i/2))
{
count++;
printf("\t%d",i);
}
if(count%5==0)
printf("\n");
}
}
getch();
Output:
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
/* GENERATE PRIME NOS. BETWEEN 1 AND 100 EXCEPT THOSE DIVISIBLE BY 5*/
#include<stdio.h>
#include<c
onio.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;
co
nt
in
ue
;
}
for
(j=2;j<=temp/2;j
++)
{
if
(temp
%j!=0)
{
f
l
a
g
=
1
;
}
els
e
{
f
l
a
g
=
0
;
b
r
e
a
k
;
}
}
if(flag==1)
{
printf
("\t
%d",temp);
}
}
OUTPUT:
2 3 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
#include<stdio.h>
#include<c
onio.h>
void
main()
{ int i,j,t;
int
n[5];
clrscr();
//to enter the
data
printf("\nEnter the
for(i=0;i<5;i++)
scanf("%d",&n[i]);
5 numbers to be sorted\n");
//the
sorting
operation
for(j=0;j<5;j++)
for(i=0;i<4
-j;i++)
if(n[i]>n[i
+1])
{t=n[i];
n[i]=
n[i+1
];
n[i+1
]=t;
}
//to
print
the
sorted
list
printf("output:");
for(i=0;i<5;i++)
printf("\n%d
",n[i]);
getch();
OUTPUT:
Enter the 5 numbers to be sorted
12
1
3
5
3
4
7
6
O
u
t
p
u
t
:
5
1
2
1
3
3
4
7
6
OUTPUT:
CALCULATION OF nCr
Enter the value of n ,r 6 2
6C2 = 15
any
p=num%10;
sum=sum+p;
num=num/10;
}
printf("\nSum of digit of
number=="); printf("%d",sum);
given
getch();
}
OUTPUT:
Enter any number==4672
Sum of digit given number==19
getch();
Output:
Enter taxable income:132000
Amount of tax on Rs.1320.000000 equals to
Rs.32600.000000
series
cos(%lf)==
}
MAIN
/*
//END OF
*/
OUTPUT:
Enter the value for x:1
Sum of sine series sin(1.000000)==0.841471
#include<stdio.h>
#include<ma
th.h>
#include<co
nio.h>
long fact
(int a);
void main()
{
double
x,sum1=0.0,sum2=0.0,sum=0.0;
int y,i;
clrscr();
printf("\nEnter
the
value
for x:"); scanf("%lf",&x);
for(i=0;i<=30;i=i+4)
sum1=sum1+
((pow(x,i))/fact(i));
for(i=2;i<=30;i=i+4)
sum2=sum2+
((pow(x,i))/fact(i));
sum=sum1-sum2;
printf("Sum of cosine series
%lf",x,sum); getch();
}
long fact
(int a)
{
int j;
lo
ng
f=
1;
if
(a
==
0)
return(1.0);
else
{
for(j=1;j<
=a;j++)
f=f*j;
ret
urn
(f)
;
}
cos(%lf)==
OUTPUT:
Enter the value for x: 2
Sum of cosine series cos (2.000000)==-1.350759
avg[
i];
roll
=i;
}
}
printf("\n The topper of the class is student %d with
average
=%f",roll,max);
getch();
OUTPUT:
Enter the number of students =3
Enter the number of subjects =5
Enter marks for 1 student Subject 1=69
Subject 2=58
Subject 3=45
Subject 4=10
Subject 5=35
Enter marks for 2 student Subject 1=47
Subject 2=25
Subject 3=16
Subject 4=97
Subject 5=46
Enter marks for 3 student Subject 1=30
Subject 2=90
Subject 3=76
Subject 4=58
Subject 5=47
The average of the 1 student = 42.400002
The average of the 2 student = 45.200001
The average of the 3 student = 59.200001
The topper of the class is student 3 with average =
59.200001
to
be
}
printf ("\n The reversed number is
%d",n2); while (n2>0)
{
rem
=n2
%10
;
n2=
n2/
10;
sum=s
um+re
m;
}
printf ("\n The sum of digits of reversed number is
%d",sum);
}
OUTPUT:
Enter the number to be
reversed =4567 The
reversed number is 7654
am
=
sum/n;
sum = 0.0;
for(i=0;i<n;i++)
sum
=
sum+((x[i]am)*(x[i]-am));
var
=
sum/n;
sd = sqrt(var);
printf("\n
ARITHMETIC
MEAN,
STD.
DEVIATION
VARIANCE ="); printf("%f\t%f\t%f",am,var,sd);
getch();
AND
OUTPUT:
Enter the no. of
terms= 5 Enter
5 values
4
9
8
6
12
Arithmetic MEAN, STD. DEVIATION and VARIANCE =7.800000
7.360000
2.712932
#include<stdio.h>
#include<c
onio.h>
#include<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]*(pow
(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
binary
number
OUTPUT:
is
mat1[i][j]
for
(i=0;i<m;i
++)
{
for
(j=0;j<n;j
++)
{
printf
("\n
The
%d\n",mat3[i][j]);
}
printf
("\n");
}
OUTPUT:
The
The
The
The
The
The
The
The
The
The
resultant
matrix
is
of
of
c
h
(
)
;
}
Output:
Enter no. of row and col of matrix a:3 3
Enter no. of row and col of matrix b:3 2
Enter matrix a
012
123
234
Enter matrix b
1 -2
-1 0
2 -1
The product of matrix is:
3 -2
5 -5
7 -8
}
Output:
Equation: x*(exp(x)-1)
Enter the app value of x1,x2:0.5
1
The root lies b/w 0.500000 &
1.000000
f(x1)
f(x2)
n
1
2
3
4
x1
0.500000
0.500000
0.500000
0.562500
x2
1.000000
0.750000
0.625000
0.625000
x3
0.750000
0.625000
0.562500
0.593750
%f",i,x1,x2,x3,p,q,r);
f(x1)
f(x2)
-0.175639
1.718282
-0.175639
0.587750
-0.175639
0.167654
-0.012782
0.167654
f(x3)
0.587750
0.167654
-0.012782
0.075142
5
6
0.562500
0.562500
0.593750
0.578125
0.578125
0.570312
-0.012782
-0.012782
0.075142
0.030619
0.030619
0.008780
0.570312
0.566406
0.570312
0.568359
0.568359
0.567383
0.567383
-0.012782
-0.002035
-0.002035
0.566895
0.008780
-0.002035
0.008780
0.003364
0.003364
0.000662
-0.002035
7
0.562500
8
0.566406
9
0.566406
10
0.566406
-0.000687
0.000662
df(x0)\n ");
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)
df(x0)
2.000000
2.372881
-4.400000
11.800000
2.372881
2.313010
1.052938
17.586615
2.313010
2.311227
0.029603
16.601265
2.311227
2.311225
0.000026
16.572248
OUTPUT:
Enter the first approximation :
0
Enter the second
approximation: 1
The 1st iteration is 0.605959
The 2nd iteration is 0.607057
The 3rd iteration is 0.607100
The answer is repeated at 3 iteration is 0.607100
be
OUTPUT:
Enter no. of terms:5
Enter
3
5
11
27
34
values x y:
13
23
899
17315
35606
is
to
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=m
x[i]
;
y0=m
y[i]
;
p=(x
x0)/
h;
sum=
y0;
for(k=1;k<
=4;k++)
{
fun=(fun*(p-(k-
be
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
20
40
60
80
100
values x y:
41
103
168
218
235
}
printf("\nwhen
x=%5.2f
%5.2f",x,y); getch();
then
Output:
Enter No. of Points: 6
Enter
x
4
5
7
y=
10
900
11
1210
13
2028
Enter the value of x at which f(x)
is required:8 when x= 8.00 then
y=445.62
ay[20],
int
number
of
for
(int
i=0;i<n;i++)
{
cout<<"Enter
(
x"<<i+1<<"
,y"<<i+1<<" )\n"; cin>>ax[i]>>ay[i];
1
;
A
=
0
;
for(j=0;j<n
-2;j++)
{
for(z=0;z<m1;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
follows:\n"; for(z=0;z<i;z++)
cout<<"\n"<<diff[z];
is
as
A=0;
for
(z=0;z<n1;z++)
{
temp*=(ax[n]-ax[z]);
ay[n]
+=temp*diff[z+A
]; A+=m-2;
m--;
}
cout<<"\n\n The value
is :"<<ay[n]; getch();
}
of
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
The difference table is as
follows:
121
2
6
5
4
5
7
7
0
for
"<<ax[n]<<"
9
2
4
3
2
4
2
1
1
0
The value of y for x = 9 is: 810
OUTPUT
Enter number of item: 4
Enter the value in the form of x
Enter
Enter
Enter
Enter
the
the
the
the
value
value
value
value
of
of
of
of
x1
x2
x3
x4
20
24
28
32
OUTPUT
Enter the number of item : 4
Enter the value in the form of x
Enter
Enter
Enter
Enter
the
the
the
the
value
value
value
value
of
of
of
of
x1
x2
x3
x4
20
24
28
32
OUTPUT:
Enter number of parts: 6
Lower and upper limits: 4 5.2
x
y
4.00000
1.38629
4.24000
1.44456
4.48000
1.49962
4.72000
1.55181
4.96000
1.60141
5.20000
1.64866
Result = 1.827570
#include<stdio.h>
#include<m
ath.h>
#include<c
onio.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
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 : 6
Enter lower and upper limits :0.2 1.4
x
y
0.20000
0.40000
0.60000
0.80000
1.00000
1.20000
1.40000
Result = 4.052133
3.02951
2.79753
2.89759
3.16604
3.55975
4.06983
4.70418
OUTPUT :
Enter number of part parts : 6
Enter lower and upper limits : 0.2 1.4
x
y
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
#include<s
tdio.h>
#include<m
ath.h>
#include<c
onio.h>
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 : 6
Enter lower and upper limits : 4 5.2
x
y
4.00000
1.38629
4.20000
4.40000
4.60000
4.80000
5.00000
5.20000
Result = 1.81427
1.43508
1.48160
1.52606
1.56862
1.60944
1.64866
OUTPUT :
Enter number of parts : 6
Enter lower and upper limits : 0.2 1.4
x
y
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.051446
OUTPUT:
Enter the elements of the augment matrix row wise :
3 1 -1 3
2 -8 1 -5
1 -2 9 8
Matrix :
3.000000
2.000000
1.000000
Solution is :
x[1]= 1.0000
x[2]= 1.0000
x[3]= 1.0000
1.000000
-8.000000
-2.000000
-1.000000
1.000000
9.000000
3.000000
-5.000000
8.000000
1 -2 9 8
The digonal matrix is :-3.000000
0.000000
0.000000
0.000000 Solution is :x[1]= 1.0000
x[2]= 1.0000
x[3]= 1.0000
0.000000
-8.666667
0.000000
0.000000
8.884615
3.000000
-8.666667
8.884615
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
x[1]= 1.0000 x[2]=-1.0000 x[3]= 1.0000
OUTPUT:
Enter the no. of
observations: 5
Enter the values of x
Enter the value of
x1 : 1
Enter the value of
x2 : 2
Enter the value of
x3 : 3
Enter the value of
x4 : 4
Enter the value of
x5 : 5
Enter the values of y :
Enter the value of
y1: 14
Enter the value of
y2: 27
Enter the value of
y3: 40
Enter the value of
y4: 55
Enter the value of
y5: 68
Equation of the STRAIGHT LINE of the form y=a+b*x is: y=0 +
13.6
38. Write a program in C for the Runge Kutta Method for solving
Differential Equations.
/* RUNGA - KUTTA METHOD */
#include<stdio.h>
#include<c
onio.h>
#include<m
ath.h>
void main()
{
float f(float x , float y);
float x0 = 0.1 , y0 = 1 , xn = 2.1 , h =0.2 , k1 , k2 ,
k3 , k4 ; int i , n ;
clrscr();
printf("\ndy/dx = (x^3 +y^2)/10 ");
printf("\ngiven y0=1 && x0= 0.1 && h=0.2 (in the range x0 < x <
2.1)\n");
n = (xn - x0) / h;
for(i = 0 ; i <= 10
;
i++)
{
k1 = h * f(x0 , y0);
k2 = h * f((x0 + h)/2 , (y0 +
k1)/2); k3 = h * f((x0 +
h)/2 , (y0 + k2)/2); k4 = h *
f(x0 + h , y0 + k3);
y0 = y0 + (1 / 6.0) * (k1 + 2*k2 + 2*k3 + k4);
printf("\nThe solution of differential equation is when x =
%f y = %f \n" , x0 , y0);
x0 = x0 + h;
}
}
getch();
f(float
y)
float g;
g = (x*x*x
y*y)
/
2
return g ;
+
;
OUTPUT:
dy/dx = (x^3 +y^2)/10
Given y0 = 1 && x0 = 0.1 && h = 0.2 (in the
range x0 < x < 2.1)
The solution of differential equation is when x
0.100000 y = 1.053870
The solution of differential equation is when x
0.300000 y = 1.116833
The solution of differential equation is when x
0.500000 y = 1.194833
The solution of differential equation is when x
0.700000 y = 1.297048
The solution of differential equation is when x
0.900000 y = 1.436797
The solution of differential equation is when x
1.100000 y = 1.633150
The solution of differential equation is when x
1.300000 y = 1.914011
The solution of differential equation is when x
1.500000 y = 2.322503
The solution of differential equation is when x
1.700000 y = 2.931453
The solution of differential equation is when x
1.900000 y = 3.880822
The solution of differential equation is when x
2.100000 y = 5.495997
=
=
=
=
=
=
=
=
=
=
=
/* ROMBERG METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define fun(x) x/sin(x)
void main()
{
int i,j,k,n,p;
float h,y[50],a,b,ih,ir[50];
clrscr();
printf("Enter the upper, lower limits and the value of n\n");
scanf("%f\t%f\t%d",&a,&b,&n);
for(i=0;i<=n;i++)
{
ih=0;
p=pow(2,i);
h=(b-a)/p;
for(j=0;j<=p;j++)
{
y[j]=a+j*h;
if(j!=0)
y[j]=y[j]/sin(y[j]);
else
y[0]=1;
if(j>0 && j<p)
ih=ih+2*(y[j]);
}
ih=ih+y[0]+y[p];
ih=ih*h/2;
ir[i]=ih;
printf("\n%f",ir[i]);
}
k=2;
while(n>=1)
{
p=pow(2,k);
for(i=0;i<n;i++)
ir[i]=(p*ir[i+1]-ir[i])/(p-1);
n--;
k=k+2;
}
printf("\nThe value of integral is %f",ir[0]);
getch();
}
scanf("%f %f",&h,&k);
sx=(b-a)/h;
sy=(d-c)/k;
for(i=0;i<=sx;i++)
x[i]=a+i*h;
for(j=0;j<=sy;j++)
y[j]=c+j*k;
for(int q=0;q<=sy;q++)
printf("\t%7.0f",y[q]);
printf("\n");
for(i=0;i<=sx;i++){
printf("%6.0f:",x[i]);
for(j=0;j<=sy;j++)
{
z[i][j]=fn(x[i],y[j]);
printf("%f ",z[i][j]);
}
A[i]=simps(z[i],sy,h);
printf("%f\n",A[i]);
}
printf("\nANS: %f",simps(A,sx,k));
getch();
}