NM Record
NM Record
NM Record
III SEMESTER
STUDENT NAME :
REGISTER NO :
YEAR/SEMESTER/SECTION :
BATCH :
TABLE OF CONTENTS
Page Staff
S.No Date Title of the Exercises Marks
no signature
ROOTS OF NON – LINEAR EQUATION USING BISECTION
1 METHOD
ROOTS OF NON – LINEAR EQUATION USING NEWTON’S
2 METHOD.
AIM:
To write a program in “C” Language to find out the root of the Algebraic and Transcendental
equations using Bisection Method.
ALGORITHM:
Step - 7. X0=(x1+x2)/2
Step - 8. If f(x0)*f(x1)<0
Step - 9. X2=x0
void main()
{
float a=-1,b=-1,c,prv=-1;
int i;
char ch;
system("cls");
printf(" A\t\tB\t\tRoot\n");
//Iterration Table
while((ceil(10000*prv)/10000)!=(ceil(10000*c)/10000))
{
//Prv checks previous iteration value, loop terminates when root and
//prev are equal
prv=c;
c=((a+b)/2);
printf(" %.4lf\t\t%.4f\t\t%.4f\n",a,b,c);
if(f(a)>f(b)) //f(a)>f(b)
{
if(f(c)>0)
a=c;
else
b=c;
}
else //f(b)>f(a)
{
if(f(c)>0)
b=c;
else
a=c;
}
}
printf("\nThe Aproximate Root is %.4f",c);
getch();
}
INPUT/OUTPUT:
f(x)=((x*x*x)-(x)-1)
RESULT:
EX.NO:2
ROOTS OF NON – LINEAR EQUATION USING NEWTON’S METHOD.
DATE:
AIM:
ALGORITHM :
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) ((3*x)-cos(x)-1) //Function
#define f1(x) (3+sin(x)) //Derivative of the function
void main()
{
float a=-1,b=-1,c,prv=-1;
int i;
char ch;
system("cls");
printf("Do you want to choose interval (y/n)?: ");
scanf("%c",&ch);
if (ch=='y'||ch=='Y')
{
printf("Enter value of A and B\n");
scanf("%f%f",&a,&b);
}
else
{
for(i=0;a==-1||b==-1;i++)
{
if(f(0)<0)
{
if(f(i)<0)
a=i;
if(f(i)>0)
b=i;
}
else
{
if(f(i)>0)
a=i;
if(f(i)<0)
b=i;
}
}
printf("\nFinding values of A and B...\nA=%.f\nB=%.f\n",a,b);
}
printf(" Itt\t\t\tRoot\n");
c=(abs(f(a))>=abs(f(b)))?a:b;
i=0;
while((ceil(10000*prv)/10000)!=(ceil(10000*c)/10000))
{
prv=c;
c=(c-(f(c)/f1(c)));
printf(" %d\t\t%.4f\n",i,c);
i++;
}
printf("\nThe Aproximate Root is %.4f",c);
}
INPUT/OUTPUT:
RESULT:
EX.NO:3
TO FIND THE LARGEST EIGEN VALUE OF A MATRIX BY POWER - METHOD.
DATE:
AIM:
To write a C program to implement the largest eigen value of a matrix by power - method.
ALGORITHM:
1. Start
5. Initialize: Lambda_Old = 1
6. Multiply: X_NEW = A * X
7. Replace X by X_NEW
11. If |Lambda_Old - Lamda_New| > e then set Lambda_Old = Lamda_New and goto
step (6) otherwise goto step (12)
12. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
int i,j,n;
float A[40][40],x[40],z[40],p[40],max;
system("cls");
printf("\nEnter the order of matrix:");
scanf("%d",&n);
printf("\nEnter matrix elements row-wise\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
printf("A[%d][%d]=", i,j);
scanf("%f",&A[i][j]);
}
}
printf("\nEnter the column vector\n");
for(i=1; i<=n; i++)
{
printf("X[%d]=",i);
scanf("%f",&x[i]);
}
while(1)
{
for(i=1; i<=n; i++)
{
p[i]=z[i];
}
for(i=1; i<=n; i++)
{
z[i]=0;
for(j=1; j<=n; j++)
{
z[i]=z[i]+A[i][j]*x[j];
}
}
max=fabs(z[1]);
for(i=2; i<=n; i++)
{
if((fabs(z[i]))>max)
max=fabs(z[i]);
}
for(i=1; i<=n; i++)
{
z[i]=z[i]/max;
}
AIM:
To write a C program to implement the system of linear equations using gauss - elimination
method.
ALGORIHTM:
1. Start
6. Display Result.
7. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int i,j,k,n;
float a[10][10], x[10], r;
system("cls");
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - r*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
printf("\nAnswer:\n");
return(0);
}
INPUT/OUTPUT:
RESULT:
EX.NO:5
SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS - JORDAN METHOD
DATE:
AIM:
To write a C program to implement the system of linear equations using gauss - jordan method.
ALGORITHM:
1. Start
6. Display Result.
7. Stop
PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
{
float a[10][ 10], x[10], ratio;
int i,j,k,n;
printf("Enter number of unknowns: ");
scanf("%d", &n);
printf("Enter coefficients of Augmented Matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f", &a[i][j]);
}
}
for(i=1;i<=n;i++)
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
exit(0);
}
for(j=1;j<=n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
for(i=1;i<=n;i++)
{
x[i] = a[i][n+1]/a[i][i];
}
/* Displaying Solution */
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
getch();
return(0);
}
INPUT/OUTPUT:
RESULT:
EX.NO:6 SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS -
DATE: SEIDAL ITERATION METHOD.
AIM:
To write a C program to implement the system of linear equations using gauss - seidal iteration
method.
ALGORITHM:
set x = X0
repeat
maxdiff: = 0
for i := 1 to n
y:=(bi−∑j=1j≠inaijxj)/aii
if | y − xi | < maxdiff then maxdiff: = | y − xi |
xi := y
next i
until maxdiff > ∈
[x is the refined solution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int main()
int count=1;
system("cls");
printf("\nCount\tx\ty\tz\n");
do
x0 = x1;
y0 = y1;
z0 = z1;
x1 = f1(x1,y1,z1);
y1 = f2(x1,y1,z1);
z1 = f3(x1,y1,z1);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
count++;
getch();
return 0;
}
NPUT AND OUTPUT:
RESULT:
EX.NO:7
FIND AREA BY USING TRAPEZOIDAL RULE
DATE:
AIM:
To write a C program to implement find area by using trapezoidal rule.
ALGORITHM:
1. Start
3. Read lower limit of integration, upper limit of integration and number of sub interval
6. Set: i = 1
13. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+pow(x,2))
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
RESULT:
EX.NO:8
FIND AREA BY USING SIMPSON’S RULE
DATE:
AIM:
ALGORITHM:
1. Start
6. Set: i = 1
9. If i mod 2 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 4 * f(k)
End If
13. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
RESULT:
EX.NO:9
FIND AREA BY USING SIMPSON’S 3/8 RULE.
DATE:
AIM:
To write a program in C to implement find area by using simpson’s 3/8 rule.
ALGORITHM:
1. Start
3. Read lower limit of integration, upper limit of integration and number of sub interval
6. Set: i = 1
9. If i mod 3 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 3 * f(k)
End If
13. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
RESULT:
EX.NO:10
TO FIND THE NUMERICAL SOLUTION OF HEAT EQUATION.
DATE:
AIM:
To write a program in C to implement to find the numerical solution of heat equation.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<math.h>
#define X 8
#define T 5
float fun(int a)
{
return(4*a-(a*a)/2.0);
}
main()
{
float u[X+1][T+1], h=1.0, k=0.0125,c,al,us,ue;
int j,i;
printf("\n Enter the square of 'c' :");
scanf("%f",&c);
al=c*k/pow(h,2);
printf(" Enter the value of u[0, t] :");
scanf("%f",&us);
printf(" Enter the value of u[%d,t]:", X);
scanf("%f",&ue);
for(i=0;i<=T;i++)
u[0][i]=u[X][i]=us;
for(j=1;j<=X-1;j++)
u[j][0]=fun(j);
for(i=0;i<=T-1;i++)
for(j=1;j<=X-1;j++)
u[j][i+1]=al*u[j-1][i]+(1-2*al)*u[j][i]+al*u[j+1][i];
printf(" The value of alpha is %4.2f\n",al);
printf(" The value of u[j,i] are:\n ");
for(i=0;i<T;i++)
{
for(j=0;j<X;j++)
printf("%7.4f\t",u[j][i]);
printf("\n");
}
}
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20
RESULT:
EX.NO:11
QUEUE USING LINKED LIST
DATE:
AIM:
To write a program in C to implement Queue using Linked List.
ALGORITHM:
INSERT()
if(front==NULL)
// Queue is empty
front=tmp;
else
rear->link=tmp;
rear=tmp;
DELETE()
{
if(front==NULL)
else
tmp=front;
free(tmp);
DISPLAY()
struct node*ptr;
ptr=front;
if(front==NULL)
else
while(ptr!=NULL)
Write(" %d ",ptr->info);
ptr=ptr->link;
Write("\n");
RESULT:
EX.NO:12
DOUBLY LINKED LIST
DATE:
AIM:
To write a program in C to implement Doubly Linked List.
ALGORITHM:
INSERT AT FIRST(X)
tmp->prev=NULL;
tmp->info=num;
tmp->next=start;
start=tmp;
INSERT AT MIDDLE(X)
int i;
q=start;
for(i=0;i<(c-1);i++)
q=q->next;
if(q==NULL)
return;
tmp=malloc(sizeof(struct node));
tmp->info=num;
q->next->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
q->next=tmp;
DELETE (X)
/ *if(start==NULL)
return;
} */
if(start->info==num)
tmp=start;
start=start->next;
start->prev=NULL;
free(tmp);
return;
q=start;
q=q->next;
tmp=q->next;
free(tmp);
q->next=NULL;
return;
if(q->next->info==num)
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
free(tmp);
return;
DISPLAY
if(start==NULL)
return;
q=start;
while(q!=NULL)
printf(" %d ",q->info);
q=q->next;
printf("\n");
} // End of display()
COUNT()
while(q!=NULL)
q=q->next;
cnt++;
} // End of count
REVERSE()
rev()
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
p2->prev=p2->next;
p2->next=p1;
p1=p2;
start=p1;
} // end of rev()
PROGRAM:
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20
RESULT:
EX.NO:13
TREE TRAVERSALS
DATE:
AIM:
To write a program in C to implement Tree Traversals
ALGORITHM:
if(r!=NULL)
printf("\t %d",r->data);
preorder(r->left);
preorder(r->right);
if(r!=NULL)
inorder(r->left);
Write("\t %d",r->data);
inorder(r->right);
{ if(r!=NULL)
{ postorder(r->left);
postorder(r->right);
printf("\t %d",r->data);
}}
PROGRAM:
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20
RESULT:
EX.NO:14
BREATH FIRST SEARCH
DATE:
AIM:
To write a program in C to implement Breath First Search.
ALGORITHM:
ALGORITHM BFS(INT V)
for(i=1;i<=n;i++)
vis[i]=0;
vis[v]=1;
u=v;
while(v<=n)
front=0;
rear=0;
for(i=1;i<=n;i++)
q[i]=0;
for(w=1;w<=n;w++)
front++;
q[front]=w;
vis[w]=1;
if((rear==0)&&(front==0))
return;
else
{
while(rear<front)
rear++;
u=q[rear];
v++;
}
PROGRAM:
INPUT/OUTPUT:
RESULT:
EX.NO:15
DEPTH FIRST SEARCH
DATE:
AIM:
To write a program in C to implement Depth First Search.
ALGORITHM:
int w;
vis[v]=1;
for(w=1;w<=n;w++)
if(vis[w]==0)
if(adj[v][w]==1)
dfs(w);
}
PROGRAM:
INPUT/OUTPUT:
RESULT:
EX.NO:16
SHELL SORT
DATE:
AIM:
To write a program in C to implement Shell Sort.
ALGORITHM:
shellSort()
A : array of items
/* calculate interval*/
interval = interval * 3 + 1
end while
valueToInsert = A[outer]
inner = outer;
while inner > interval -1 && A[inner - interval] >= valueToInsert do:
end while
A[inner] = valueToInsert
end for
/* calculate interval*/
end while
end procedure
PROGRAM:
INPUT / OUTPUT:
RESULT: