You are on page 1of 75

//GREATEST OF THE THREE

NUMBERS//
#include<stdio.h>
#include<conio.h>
int main()
{
float a,b,c;
printf("Enter a, b and c\n");
scanf("%f %f %f",&a,&b, &c);
if(a>b && a>c)
{
printf("a=%f is the
greatest",a);
}else {
if (b>a && b>c)
{
printf("b=%f is the
greatest",b);
NACP LAB REPORT

Page 1

}
else
{
printf("c=%f is the
greatest",c);
}
}
getch();
return 0;
}

NACP LAB REPORT

Page 2

//FACTORIAL OF A NUMBER//
#include<stdio.h>
#include<conio.h>
int main()
{
int number, i, fact=1;
printf("Enter the number to
calculate the factorial\n");
scanf("%d", &number);
if(number<0)
{
printf("Enter a non-negative
integer");
}else{
for(i=1; i<=number; i++)
{
fact=fact*i;
}

NACP LAB REPORT

Page 3

printf("The factorial of the


number is %d", fact );
}
getch();
return 0;
}

NACP LAB REPORT

Page 4

//MAGIC SQUARE//
#include<stdio.h>
#include<conio.h>
main()
{
int size=4;
int matrix[4][4]=
{{4,15,14,1},{9,6,7,12},{5,10,11,8},{
16,3,2,13}};
int row, column=0;
int sum, sum1, sum2;
int flag=0;

//for diagonals//
sum=0;
for(row=0; row<size; row++)
{
for(column=0; column<size;
column++)
if(row==column)
NACP LAB REPORT

Page 5

sum=sum+matrix[row][column];
}

//for rows//
for(row=0; row<size; row++)
{
sum1=0;
for(column=0; column<size;
column++)
{
sum1=sum1+matrix[row][column];
}
if(sum==sum1)
{
flag=1;
}
else
{
flag=0;
break;
NACP LAB REPORT

Page 6

}
}

//for column//
for(row=0; row<size; row++)
{
sum2=0;
for(column=0; column<size;
column++)
{
sum2=sum2+matrix[column][row];
}
if(sum==sum2)
flag=1;
else
{
flag=0;
break;
}
NACP LAB REPORT

Page 7

if (flag==1)
printf("\n The matrix is a magic
square");
else
printf("\n The matrix is not a
magic square");

NACP LAB REPORT

Page 8

//PROGRAM TO FIND THE SIMPLE AND


COMPOUND INTEREST//
#include<stdio.h>
void main()
{
float p,n,r,si,ci;
clrscr();
printf("Enter the values for P,N,R");
scanf("%f%f%f",&p,&n,&r);
si=p*n*r/100;
ci=p*pow((1+r/100),n)-p;
printf("Simple Interest = %.2f",si);
printf("\nCompound Interest =
%.2f",ci);
getch();
}

NACP LAB REPORT

Page 9

//Quadratic equation//
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
int D;
int root1,root2;
clrscr();
printf("enter values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
D=sqrt((b*b)-(4*a*c));
root1=(-b+D)/(2*a);
root2=(-b-D)/(2*a);
printf("\n Root1=%d",root1);
printf("\n Root2=%d", root2);
getch();
}

NACP LAB REPORT

Page 10

//FIND THE SUM & SUBTRACTION OF TWO


INTEGERS WITH THE HELP OF FUNCTION//
#include<stdio.h>
#include<conio.h>
int add(int,int);
int sub(int,int);
void main()
{
int c,d;
int a,b;
clrscr();
printf("\n Enter two numbers\n");
scanf("%d%d",&a,&b);
c=add(a,b);
d=sub(a,b);
printf("The value of c=%d",c);
printf("\nThe value of d=%d",d);
getch();
}
int add(int x,int y)
{
return(x+y);
}
int sub(int p,int q)
{
int r;
r=p-q;
return r;
}

NACP LAB REPORT

Page 11

//RECORD EMPLOYEE DETAILS USING STRUCTURE//


#include<stdio.h>
#include<conio.h>
struct emp
{
int id;
char name[15];
char job[15];
float salary;
}e;
void main()
{
clrscr();
printf("Enter Emp Id:\n");
scanf("%d",&e.id);
printf("Enter emp name:\n");
scanf("%s", &e.name);
printf("Enter emp job:\n");
scanf("%s",&e.job);
printf("Enter emp salary:\n");
scanf("%d",&e.salary);
printf("\n\n\t The record is as following \n\n");
printf("Your ID is:%d",e.id);
printf("\n\n Your name is:%s",e.name);
printf("\n\nYour job is:%s",e.job);
printf("\n\n Your salary is:%d", e.salary);
getch();
}

OUTPUT:
Enter emp Id:
22345
Enter emp Name:
Victor
Enter emp Job:
Programmer
Enter emp salary:
35000
The record is as following
Your ID is: 22345
Your name is: Victor
Your job is: Programmer
Your salary is15000
NACP LAB REPORT

Page 12

//PROGRAM TO SWAP THE VALUES//


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("enter two values \n");
scanf("%d%d", &a,&b);
printf("\n before swapping value
as:\n\n");
printf("a= %d",a);
printf("\n b=%d",b);
printf("\n\n after swapping value
are:\n");
temp=a;
a=b;
b=temp;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Enter the value of a and b
12
67
Before Swapping
a = 34
b = 45
After Swapping
a = 45
b = 34

NACP LAB REPORT

Page 13

//SWAP TWO NUMBERS USING FUNCTIONS//


#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter the values for a&b:");
scanf("\n%d%d", &a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\n after swapping the value for a &
b is: %d %d",a,b);
}

NACP LAB REPORT

Page 14

//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);
getch();
}

NACP LAB REPORT

Page 15

//PROGRAM TO FIND PRODUCT OF 1ST 'N'


NATURAL NUMBERS //
#include<stdio.h>
#include<conio.h>
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(i<n)
goto a5;
printf("\nProduct of 1st %d natural
numbers:\t%ld",n,prod);
getch();
}

NACP LAB REPORT

Page 16

//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();
}

NACP LAB REPORT

Page 17

//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
1 and 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);
}
NACP LAB REPORT

Page 18

if(count%5==0)
printf("\n");
}
getch();
}

NACP LAB REPORT

Page 19

//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
{

NACP LAB REPORT

Page 20

flag=0;
break;
}
}
if (flag==1)
{
printf ("\t %d",temp);
}
}
getch();
}

NACP LAB REPORT

Page 21

//CALCULATION OF SINE SERIES //


#include<stdio.h>
#include<math.c>
#include<conio.h>
int 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=1;i<=30;i=i+4)
sum1=sum1+((pow(x,i))/fact(i));
for(i=3;i<=30;i=i+4)
sum2=sum2+((pow(x,i))/fact(i));
sum=sum1-sum2;
printf("sum of cosine series
cos(%lf)==%lf",x,sum);
getch();
} //END OF
MAIN
/* FUNCTION SUB PROGRAM */
int fact (int a)
{
int f=1,j;
NACP LAB REPORT

Page 22

if(a==0)
return(1);
else
{
for(j=1;j<=a;j++)
f=f*j;
return(f);
}
}

NACP LAB REPORT

Page 23

//CALCULATION OF COSINE SERIES //


#include<stdio.h>
#include<math.h>
#include<conio.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
cos(%lf)==%lf",x,sum);
getch();
}
long fact (int a)
{
int j;
long f=1;
if(a==0)
return(1.0);
else
{
NACP LAB REPORT

Page 24

for(j=1;j<=a;j++)
f=f*j;
return(f);
}
}

NACP LAB REPORT

Page 25

//PROGRAM TO FIND OUT AVERAGE MARKS OF


STUDENTS//
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int stu[20][20],i,j,m,n,roll=0;
float avg[20],sum=0.0,max=0.0;
clrscr();
printf("\n Enter the number of students
=");
scanf("%d",&n);
printf ("\n Enter the number of subjects
=");
scanf ("%d",&m);
for(i=1;i<=n;i++)
{
printf("\n Enter marks for %d student
=",i);
for(j=1;j<=m;j++)
{
printf("\n Subject %d",j);
scanf("%d",&stu[i][j]);
}
}
for(i=1;i<=n;i++)
{
printf("\n The average of the %d student
=\n",i);
sum=0.0;
avg[i]=0.0;
for(j=1;j<=m;j++)
{
NACP LAB REPORT

Page 26

sum=sum+stu[i][j];
}
avg[i]=(sum/m-1);
printf("%f\n",avg[i]);
if(avg[i]>max)
{
max=avg[i];
roll=i;
}
}
printf("\n The topper of the class is
student %d with average =%f",roll,max);
getch();
}

NACP LAB REPORT

Page 27

//STANDARD DEVIATION AND


VARIATION //
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n;
float x[20],sum;
float am,var,sd;
int i;
printf("\n enter the no. of
terms=");
scanf("%d",&n);
printf("\n enter %d values
\n",n);
for(i=0;i<n;i++)
scanf("%f",&x[i]);
sum=0.0;
for(i=0;i<n;i++)
sum = sum+x[i];
am = sum/n;
sum = 0.0;
for(i=0;i<n;i++)
sum = sum+((x[i]-am)*(x[i]-am));
NACP LAB REPORT

Page 28

var = sum/n;
sd = sqrt(var);
printf("\n ARITHMETIC MEAN, STD.
DEVIATION AND VARIANCE =");
printf("%f\t%f\t%f",am,var,sd);
getch();
}

NACP LAB REPORT

Page 29

//ADDTION OF MATRIX//
#include<stdio.h>
#include<conio.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]);
}
}
NACP LAB REPORT

Page 30

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");
}
getch();
}

NACP LAB REPORT

Page 31

//MULTIPLICATION OF MATRIX//
#include<stdio.h>
#include<conio.h>
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<m;i++)
{ for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
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++)
NACP LAB REPORT

Page 32

{ 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();
}

NACP LAB REPORT

Page 33

//BISECTION METHOD//
#include<stdio.h>
#include<conio.h>
#include<math.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);
NACP LAB REPORT

Page 34

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);}
else goto l15;
}
float fun(float m)
{float g;
g=(m*(exp(m))-1);
return(g); }

NACP LAB REPORT

Page 35

//NEWTON RAPHSON//
#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 f(x0) df(x0)\n ");
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);}
NACP LAB REPORT

Page 36

else
{x0=x1;
goto a10;}
}
/* Function sub program */
float f(float a)
{float g;
g = pow(a,3)+((1.2)*(pow(a,2)))-5*a7.2;
return(g);}
float df(float a)
{float g1;
g1 = (3*pow(a,2))+(2.4*a)-5;
return(g1);
}

NACP LAB REPORT

Page 37

//REGULA-FALSI METHOD//
#include<stdio.h>
#include<conio.h>
#include<math.h>
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(x2)<0.0)
{
m=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
while(fabs(f(m))>=0.0001)
{
c++;
if(f(x1)*f(m)<0.0)
x2=m;
else
x1=m;
m=((x1*f(x2))-(x2*f(x1)))/(f(x2)f(x1));
NACP LAB REPORT

Page 38

printf("\n\n the %d,ilteration is %f


",c,m);
}
printf("\n\n the answer is repeated
at %d ilteration is %f",c,m);
}
else
printf("enter valid initial
approximation :");
getch();
}

NACP LAB REPORT

Page 39

//NEWTON GREGORY FORWARD


INTERPOLATION//
#include<stdio.h>
#include<conio.h>
#include<math.h>
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<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][j1];
i=0;
do
{i++;
NACP LAB REPORT

Page 40

}while(mx[i]<x);
i--;
p=(x-mx[i])/h;
y1=p*diff[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[i3][4]/24;
y=my[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f,y=%6.8f",x,y);
getch();
}

NACP LAB REPORT

Page 41

//NEWTON GREGORY BACKWARD


INTERPOLATION//
#include<stdio.h>
#include<conio.h>
#include<math.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");
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][j1];
i=0;
while(!mx[i]>x)
i++;
NACP LAB REPORT

Page 42

x0=mx[i];
y0=my[i];
p=(x-x0)/h;
sum=y0;
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();
}

NACP LAB REPORT

Page 43

//LAGRANGE METHOD OF
INTERPOLATION//
#include<stdio.h>
#include<conio.h>
#include<math.h>
#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<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*=ax[i]-ax[j];
}
NACP LAB REPORT

Page 44

y+=(nr/dr)*ay[i];
}
printf("\nwhen x=%5.2f then
y=%5.2f",x,y);
getch();
}

NACP LAB REPORT

Page 45

//NEWTON DIVIDED DIFFERENCE METHOD//


#include<iostream.h>
#include<conio.h>
#include<math.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++)
{
NACP LAB REPORT

Page 46

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++)
cout<<"\n"<<diff[z];
// now calculating value of y for x
ay[n]=ay[0];
m=n;
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();
}

NACP LAB REPORT

Page 47

//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");
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]);

NACP LAB REPORT

Page 48

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;
}

NACP LAB REPORT

Page 49

//SIMPSON 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 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]);

NACP LAB REPORT

Page 50

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;
}

NACP LAB REPORT

Page 51

//SIMPSON 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();
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];
else
k2 = k2 + 3 * y[i];
}
NACP LAB REPORT

Page 52

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;
}

NACP LAB REPORT

Page 53

//WEDDLE'S RULE//
#include<stdio.h>
#include<math.h>
#include<conio.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 % 6 == 0)
k1 = k1 + 2 * y[i];
else if(i % 3 == 0)
k1 = k1 + 6 * y[i];
else if(i % 2 == 0)
k1 = k1 + y[i];
else
k4 = k4 + 5 * y[i];
}
NACP LAB REPORT

Page 54

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;
}

NACP LAB REPORT

Page 55

//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;
}
NACP LAB REPORT

Page 56

//now performing back substitution


for(i = n -1 ; i >= 0 ; i--)
{ s = 0;
for(j = i + 1 ; j < n ; j++)
s += matrix[i][j] * x[j];
x[i] = (matrix[i][n] - s) /
matrix[i][i];
}
//now printing the result
printf("\nSolution is :-\n");
for(i = 0 ; i < n ; i++)
printf("\nx[%d]=%7.4f" , i+1 ,x[i]);
getch();
}

NACP LAB REPORT

Page 57

//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");
NACP LAB REPORT

Page 58

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++)
printf("\nx[%d]=%7.4f" , i+1
,matrix[i][n]/matrix[i][i]);
getch();
}

NACP LAB REPORT

Page 59

//GAUSS SIEDAL 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]);
printf ("\n Enter allowed error, max
iteration=");
scanf ("%f %d",&aerr,&maxitr);
printf ("\n Iteration");
for (i=1;i<=n;i++)
printf ("\n x[%d]",i);
for (itr=1;itr<=maxitr;i++)
{
maxerr=0;
for (i=1;i<=n;i++)
{
s=0;
for (j=1;j<=n;j++)
NACP LAB REPORT

Page 60

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);
} }
}

NACP LAB REPORT

Page 61

//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];
NACP LAB REPORT

Page 62

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);
}

NACP LAB REPORT

Page 63

//RUNGE-KUTTA METHOD//
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float f(float x , float y);
float x0 = 0.1 , y0 = 1 , xn = 2,
h =0.2 , k1 , k2 , k3 , k4 ;
int i , n ;
clrscr();
printf("\ndy/dx = (x^2 +y^2)/10
");
printf("\ngiven y0=1 && x0= 0.1 &&
h=0.2 (in the range x0 < x <
2.0)\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);
NACP LAB REPORT

Page 64

printf("\nThe solution of
differential equation is when x =
%f y = %f \n" , x0 , y0);
x0 = x0 + h;
}
getch();
}
Function sub program
float f(float x , float y)
{
float g;
g = (x*x + y*y) / 10 ;
return g ;
}

NACP LAB REPORT

Page 65

//Program to calculate value of


the given function
exp(x*x*x*sin(2*x)from 0.0 to
1.2 using Simpsons 1/3 and 3/8
rule respectively//
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[15],y[15],h,k1,k2,k3,k4,k5,k6;
int i,j,n;
clrscr();
printf("Enter value of n:\n");
scanf("%d",&n);
k1=k2=0.0,x[0]=0.0,x[n]=1.2,k4=0.0,k5=0.0;
h=(x[n]-x[0])/n;
for(i=1;i<n;i++)
x[i]=x[0]+i*h;
for(i=0;i<=n;i++)
y[i]=exp(pow(x[i],3)*sin(2*x[i]));
for(i=1;i<n;i++)
{
if(i%2==0)
k1=k1+y[i];
else
k2=k2+y[i];
}
k3=h*(y[0]+y[n]+2*k1+4*k2)/3;
printf("Value of integral using Simpsons 1/3
rule");
printf("%f",k3);
NACP LAB REPORT

Page 66

/*Simpsons 3/8 rule is as follows*/


for(j=1;j<n;j++)
{
if(j%3==0)
k4=k4+y[j];
else
k5=k5+y[j];
}
k6=((3.0*h)*(y[0]+y[n]+2*k4+3*k5))/8;
printf("\nValue of integral using Simpsons
3/8 rule is %f",k6);
getch();
}

NACP LAB REPORT

Page 67

//Parabolic fit by least


squares//
#include<stdio.h>
#include<conio.h>
float arr[3][4],x,y,xx,a,b,c;
int n;
void initialize()
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
arr[i][j]=0;
}
void readdata()
{
int i;
for(i=0;i<n;i++)
{
printf("Pair num %d\n",i+1);
scanf("%f %f",&x,&y);
xx=x*x;
arr[0][1]+=x;
arr[0][2]+=xx;
arr[1][2]+=x*xx;
arr[2][2]+=xx*xx;
arr[0][3]+=y;
arr[1][3]+=x*y;
arr[2][3]+=xx*y;
}
arr[1][0]=arr[0][1];
arr[1][1]=arr[0][2];
arr[2][0]=arr[1][1];
arr[2][1]=arr[1][2];
}
void displayaugmentedmatrix()
{
int i,j;
printf("The augmented matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
NACP LAB REPORT

Page 68

{
printf("%9.4f",arr[i][j]);
}
printf("\n");
}
}
void applygaussjordon()
{
int i,j,k;
float t;
for(j=0;j<3;j++)
for(i=0;i<3;i++)
if(i!=j)
{
t=arr[i][j]/arr[j][j];
for(k=0;k<4;k++)
arr[i][k]-=arr[j][k]*t;
}
a=arr[0][3]/arr[0][0];
b=arr[1][3]/arr[1][1];
c=arr[2][3]/arr[2][2];
}
void displaybestfit()
{
printf("y=%f",a);
if(b>=0)
printf("+");
printf("%f x",b);
if(c>0)
printf("+");
printf("%f x^2",c);
}
void main()
{
clrscr();
printf("Enter the number of pairs of observed
values\n");
scanf("%d",&n);
initialize();
arr[0][0]=n;
readdata();
displayaugmentedmatrix();
NACP LAB REPORT

Page 69

applygaussjordon();
printf("The best fit curve is\n");
displaybestfit();
}

Output:
Enter the number of pairs of observed values
5
Pair num 1
0
1
Pair num 2
1
1.8
Pair num 3
2
1.3
Pair num 4
3
2.5
Pair num 5
4
6.3
The augmented matrix is
5.0000 10.0000 30.0000 12.9000
10.0000 30.0000 100.0000 37.1000
30.0000 100.0000 354.0000 130.3000
The best fit curve is
y=1.420001-1.070003 x+0.550001 x^2

NACP LAB REPORT

Page 70

//MILNE'S

PREDICTOR CORRECTOR//

# include<stdio.h>
# include<conio.h>
# include<math.h>
float x[5],y[5],h;
float f(int i)
{
return x[i]-y[i]*y[i];
}
void correct()
{
y[4]=y[2]+(h/3)*(f(2)+4*f(3)+f(4));
printf("%23s %8.4f %8.4f
\n","",y[4],f(4));
}
main()
{
float xr,aerr,yc;
int i;
clrscr();
puts("Enter the values of
x0,xr,h,allowed error");
scanf("%f %f %f
%f",&x[0],&xr,&h,&aerr);
puts("Enter the values of y[i],i=0,3");
for(i=0;i<=3;i++) scanf("%f",&y[i]);
for(i=1;i<=3;i++) x[i]=x[0]+i*h;
puts(" x Predicated Corrected");
puts(" y f y f");
while(1)
NACP LAB REPORT

Page 71

{
if(x[3]==xr) return;
x[4]=x[3]+h;
y[4]=y[0]+(4*h/3)*(2*(f(1)+f(3))-f(2));
printf("%6.2f %8.4f
%8.4f\n",x[4],y[4],f(4));
correct();
while(1)
{
yc=y[4];
correct();
if(fabs(yc-y[4])<=aerr)break;
}
for(i=0;i<=3;i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}
}
getch();
}

NACP LAB REPORT

Page 72

//NUMERICAL DOUBLE INTEGRATION//


#include<stdio.h>
#include<conio.h>
#include<math.h>
float func(float x, float y)
{
//return(1/sqrt(pow(x,2)+pow(y,2)));
return(sin(x*y)/(1+x*y));
}
void main()
{
int i,j,x,y;
float h,k,a,b,c,d,xy[20][20],ax[20],ans;
clrscr();
printf("Integral a to b, integral c to d
(function) dxdy");
printf("\nEnter a,b,c,d,h,k where h,k are
number of parts for a,b & c,d resp\n");
fflush(stdin);
scanf("%f %f %f %f %f
%f",&a,&b,&c,&d,&h,&k);
x=((b-a)/h)+0.5;
y=((d-c)/k)+0.5;
printf("\nx=%d,y=%d\n",x,y);
for(i=0;i<=x;i++)
{
for(j=0;j<=y;j++)
{
xy[i][j]=func(a+i*h,c+j*k);
printf("%0.4f ",xy[i][j]);
}
printf("\n");
}
NACP LAB REPORT

Page 73

for(i=0;i<=x;i++)
{
ax[i]=0;
for(j=0;j<=y;j++)
{
if(j==0 || j==y)
ax[i]+=xy[i][j];
else if(j%2==1)
ax[i]+=4*xy[i][j];
else
ax[i]+=2*xy[i][j];
}
ax[i]=(h/3)*ax[i];
printf("\nAx(%d)=%f",i,ax[i]);
}
ans=0;
for(i=0;i<=x;i++)
{
if(i==0 || i==x)
ans+=ax[i];
else if(i%2==1)
ans+=4*ax[i];
else
ans+=2*ax[i];
}
ans=(k/3)*ans;
printf("\nValue of integral is %f",ans);
getch();
}

NACP LAB REPORT

Page 74

//ROMBERG INTEGRATION//
#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();
}
NACP LAB REPORT

Page 75

You might also like