C&DS LabManual
C&DS LabManual
Program 1(a):-
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,s=0,r;
clrscr( );
printf(“enter any number”);
scanf(“%d”,&n);
while(n>0)
{
r=n%10;
s=s+r;
n/=10;
}
printf(“sum=%d”,s);
getch( );
}
1
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 1(b):-
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,n;
clrscr( );
printf("enter the value of n: \t");
scanf("%d",&n);
a=0;
b=1;
printf("%d\n%d\n",a,b);
c=a+b;
do
{
printf("%d ",c);
a=b;
b=c;
c=a+b;
}
while(c<=n);
getch( );
}
2
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 1(c):-
void main( )
{
int i,j,n,;
clrscr( );
printf(“enter any number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
count++;
}
if(count==0)
printf(“%d\t”,i);
}
getch( );
}
3
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 2(a):-
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int i,n,fact,x;
float sum=1;
clrscr( );
printf("Enter the Value\n");
scanf("%d",&n);
printf("Enter the value of x:\n");
scanf("%d",&x);
for(i=2;i<=2*(n-1);i+=2)
{
fact*=i*(i-1);
sum*(-1)i=pow(x,i)/(fact,i);
}
printf("Result= %f",sum);
getch( );
}
Program 2(b):-
Aim: A Program to find roots of a quadratic equation.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,d;
float r1,r2;
clrscr( );
printf(“enter a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=((-b+sqrt(d))/(2*a));
r2=((-b-sqrt(d))/(2*a));
printf(“roots are %f,%f”,r1,r2);
}
else if (d==0)
{
r1=r2=(-b/(2*a));
printf(“roots are %f,%f”,r1,r2);
}
else
{
printf(“roots are imaginary”);
}
getch( );
}
Input: enter a,b,c
5
5
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
6
8
Output: roots are imaginary
Program 3(a):-
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main( )
{
int n,k;
printf(“enter any number\n”);
scanf(“%d”,&n);
k=factorial(n);
printf(“factorial of %d is %d”,n,k);
getch( );
}
int factorial(int x)
{
int fact;
if(x==0||x==1)
return(1);
else
{
fact=(x*factorial(x-1));
return(fact);
}
}
Program 3(b):-
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,n,fact=1;
clrscr( );
printf(“enter any number\n”);
scanf(“%d”,&n); 2
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“factorial of %d is %d”,n,fact);
getch( );
}
7
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 4(a):-
#include<stdio.h>
#include<conio.h>
void main( )
{
int l,m,n;
clrscr( );
printf("enter any two values");
scanf("%d%d",&m,&n);
do
{
l=n%m;
n=m;
m=l;
}
while(l!=0);
printf("gcd is %d",n);
getch( );
}
8
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 4(b):-
#include<stdio.h>
#include<conio.h>
int gcd(int a,int b);
void main( )
{
int a,b;
clrscr( );
printf("enter any two values");
scanf("%d%d",&a,&b);
printf("gcd is %d",gcd(a,b));
getch( );
}
int gcd(int a,int b)
{
int x,y;
x=a;
y=b;
if(y!=0)
{
x=gcd(y,x%y);
}
return(x);
}
Input: enter any two values
6
24
9
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Outpuftgbbn b t: gcd is 6
Program 5(a):
#include <stdio.h>
#include <conio.h>
#include <dos.h>
typedef unsigned int uint;
#define NUM_ELEMENTOS 3
int torres[NUM_ELEMENTOS][3];
void mostraTela()
{
int j, k, posx, posy;
int i;
clrscr( );
for(j = 0; j < 3; j++)
{
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
posy = 20 - i;
posx = 20 + (j * 20);
for(k = 1; k <= torres[i][j]; k++)
{ gotoxy(posx-k, posy);
putch('-');
gotoxy(posx+k, posy);
putch('-');
}
gotoxy(posx, posy);
if(torres[i][j])
printf("%d", torres[i][j]); else
putch('|');
10
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
}
11
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
if(torres[i][src])
{
is = i;
break;
}
}
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
if(torres[i][dst]) {
id = i;
break;
}
}
12
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void ranoi(uint n, uint src, uint dst)
{
uint i, j, jog = 0, stemp, dtemp, peca,auxtemp;
int sentido = 1;
if((n % 2) == 0)
sentido = -1;
peca = 1;
for(i = 0; i < jog; i++)
{
stemp = ondeesta(peca);
if(peca % 2)
{
dtemp = ((3 + stemp) + sentido) % 3;
}
else
{
if(podemover(peca, stemp, (stemp + 1)%3))
dtemp = (stemp + 1)%3;
else
dtemp = (stemp + 2)%3;
}
move(stemp, dtemp);
peca = (peca % n) + 1;
stemp = ondeesta(peca);
while(!podemover(peca, stemp, (stemp + 1)%3) && !podemover(peca,
stemp, (stemp + 2)%3))
13
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
peca = (peca % n) + 1;
stemp = ondeesta(peca);
}
}
}
int main( )
{
uint i, j, src = 0, dst = 1;
torres[i][j] = NUM_ELEMENTOS - i;
else
torres[i][j] = 0;
}
}
mostraTela( );
gotoxy(1,1);
getch( );
ranoi(NUM_ELEMENTOS, src, dst);
gotoxy(1,1);
getch( );
return 0;
}
14
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output:
-1- | |
--2-- | |
---3--- | |
| | |
--2-- | |
---3--- -1- |
| | |
| | |
---3--- -1- --2—
| | |
| | -1-
---3--- | --2—
| | |
| | -1-
| ---3--- --2--
| | |
| | |
15
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
-1- ---3--- --2—
| | |
| --2-- |
-1- ---3--- |
| -1- |
| --2-- |
| ---3--- |
Program 5(b):-
#include<stdio.h>
#include<stdlib.h>
#define N 3
int A[N], B[N], C[N];
void Hanoi(int,int*,int*,int*);
void PrintAll(void)
{
int i;
printf("A: ");
for(i=0;i<N;i++)printf(" %d ",A[i]);
printf("\n");
printf("B: ");
16
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i=0;i<N;i++)printf(" %d ",B[i]);
printf("\n");
printf("C: ");
for(i=0;i<N;i++)printf(" %d ",C[i]);
printf("\n");
printf("-----------------------------------------\n");
return;
}
int Move(int *source, int *dest)
{
int i=0,j=0;
while((*(source + i)==0)&&(i<N))i++;
while((*(dest + j)==0)&&(j<N))j++;
*(dest+j-1) = *(source+i);
*(source + i) = 0;
PrintAll( );
return *(dest+j-1);
}
void Hanoi(int n,int *source, int *dest, int *spare)
{
int i;
if(n==1){
Move(source,dest);
return;
}
Hanoi(n-1,source,spare,dest);
Move(source,dest);
Hanoi(n-1,spare,dest,source);
return;
17
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
int main( )
{
int i;
for(i=0;i<N;i++)A[i]=i+1;
for(i=0;i<N;i++)B[i]=0;
for(i=0;i<N;i++)C[i]=0;
printf("Solution of Tower of Hanoi Problem with
%d Disks\n\n",N);
printf("Starting state:\n");
PrintAll( );
printf("\n\nSubsequent states:\n\n");
Hanoi(N,A,B,C);
return 0;
}
Starting state:
A: 1 2 3
B: 0 0 0
C: 0 0 0
------------------------------------------
Subsequent states:
A: 0 2 3
B: 0 0 1
C: 0 0 0
------------------------------------------
A: 0 0 3
B: 0 0 1
18
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
C: 0 0 2
------------------------------------------
A: 0 0 3
B: 0 0 0
C: 0 1 2
------------------------------------------
A: 0 0 0
B: 0 0 3
C: 0 1 2
------------------------------------------
A: 0 0 1
B: 0 0 3
C: 0 0 2
------------------------------------------
A: 0 0 1
B: 0 2 3
C: 0 0 0
------------------------------------------
A: 0 0 0
B: 1 2 3
C: 0 0 0
------------------------------------------
Program 6(a):-
Program 6(b):-
20
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
printf("Enter the value of b \t \n");
scanf("%d",&b);
printf("***********");
printf("\n MENU \n");
printf("***********");
printf("\n 1.Addition \n");
printf("\n 2.Subtraction \n");
printf("\n 3.Multiplication \n");
printf("\n 4.Division \n");
printf(“\n 5.Exit\n”);
printf("\n Enter ur choice\t\n") ;
while(ch!=5)
{
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("The sum is: %d",c);
break;
case 2:
c=a-b;
printf("The difference is: %d",c);
break;
case 3:
c=a*b;
printf("The Product is: %d",c);
break;
case 4:
c=a/b;
printf("The quotient is: %d",c);
break;
21
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
getch();
}
Program 7(a):-
Program 7(b):-
#include<stdio.h>
#include<conio.h>
void add( );
void mul( );
void main( )
{
clrscr( );
23
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
add( );
mul( );
getch( );
}
void add( )
{
int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr( );
printf(“enter order of first matrix”);
scanf(“%d%d”,&m,&n);
printf(“enter order of second matrix”);
scanf(“%d%d”,&p,&q);
if(m==p&&n==q)
{
printf(“enter elements of first matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements of second matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(“the addition matrix is\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,c[i][j]);
24
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
printf(“\n”);
}
}
else
printf(“addition is not possible”);
}
void mul( )
{
int i,j,k,m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr( );
printf(“enter order of first matrix”);
scanf(“%d%d”,&m,&n);
printf(“enter order of second matrix”);
scanf(“%d%d”,&p,&q);
if(n==p)
{
printf(“enter elements of first matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements of second matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
25
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
printf(“the resultant matrix is\n”);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}
else
printf(“multiplication is not possible”);
}
26
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
2 2
enter elements of first matrix
3 2
5 6
enter elements of second matrix
2 5
9 3
Output: the addition matrix is
9 11
12 8
the resultant matrix is
24 21
64 43
Program 8:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void insert( );
void delet( );
27
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void main( )
{
clrscr( );
insert( );
delet( );
getch( );
}
void insert( )
{
char str1[10],str2[10],str3[10];
int i,j,k,pos;
printf("enter main string");
scanf("%s",str1);
printf("enter position");
scanf("%d",&pos);
printf("enter sub string");
scanf("%s",str2);
for(i=0;i<pos;i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';i++,j++)
{
str3[i]=str2[j];
}
for(k=pos;str1[k]!='\0';i++,k++)
{
str3[i]=str1[k];
}
str3[i]='\0';
printf("the resultant string is\n");
printf("%s",str3);
}
void delet( )
28
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
char str1[30],str2[10];
int i,j,pos,n,m;
printf("enter string");
scanf("%s",str1);
m=strlen(str1);
printf("enter position and no.of characters to be
deleted");
scanf("%d%d",&pos,&n);
for(j=pos-1;j<(pos+n-1);j++)
{
str1[j]='\0';
}
for(j=pos-1,i=(pos+n-1);i<m;i++,j++)
{
str1[j]=str1[i];
}
str1[j]='\0';
printf("resultant string is\n");
printf("%s",str1);
}
29
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 9:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
30
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
char s1[10],s2[10];
clrscr( );
printf(“enter any string”);
scanf(“%s”,s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(“given string is a palindrome”);
else
printf(“not palindrome”);
getch( );
}
Program 10(a):-
Program 10(b):-
Aim: A Program to count the lines, words and characters in the given
text.
#include <stdio.h>
#include <conio.h>
void main( )
{
char c,choice;
32
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
char str[10];
int nc=0,nw=1,nl=0,count=0,i;
clrscr( );
printf("ENTER STRING:- ");
while ((c=getchar())!=EOF)
{
if(c==' '||(c>=65&&c<=90)||(c>=97&&c<=122))
nc++;
else if(c=='\n')
nl++;
if(c==' '||c=='\n')
nw++;
}
printf("no. of characters is %d",nc);
printf("no. of words is %d",nw);
printf("no. of lines is %d",nl);
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&choice);
getch( );
}
Input: ENTER STRING:-
abc def
ghi
Output: no. of characters is 10
no. of words is 3
no. of lines is 2
Program 11(a):-
33
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
int a[10][10];
int i,j,c,n;
clrscr( );
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
printf("%5d",a[1][1]);
a[2][1]=1;a[2][2]=2;a[2][3]=1;
printf("%d %d %d",a[2][1],a[2][2],a[2][3]);
for(i=3;i<=n;i++)
{
a[i][1]=1;
printf("%d",a[i][1]);
j=2;c=3;
while(j<=i)
{
a[i][j]=a[i-1][c-1]+a[i-1][c-2];
printf("%5d",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d", a[i][j]);
}
getch( );
}
34
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 11(b):-
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,k,l=1;
clrscr( );
printf(“enter number of rows”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i);j++)
printf(“ “);
for(k=1;k<=(2*i-1);k++)
{
if(k%2==0)
printf(“ “);
else
printf(“%d”,l++);
}
Printf(“\n”);
}
getch( );
}
36
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 12:-
Aim: A Program to read two numbers x and n and then compute the sum
of this geometrix progression.
1+x+x2+x3+…………+xn.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int i,sum=0,x,n;
printf("enter the values of x and n");
scanf("%d%d",&x,&n);
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("result of the expression is %d",sum);
getch( );
}
Program 13:-
37
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],b[10],i,j,n;
clrscr( );
printf("enter no. of digits");
scanf("%d",&n);
printf("enter the binary number");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1,j=0;i>=0;i--,j++)
{
if(a[i]!=1)
{
b[j]=a[i];
}
else
{
b[j]=a[i];
--i;
++j;
for(;i>=0;i--,j++)
{
if(a[i]==0)
b[j]=1;
else
b[j]=0;
}
}
}
Program 14:-
#include<stdio.h>
39
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main( )
{
clrscr( );
int *a,l,i,j,k;
char *s;
printf("Enter The Roman Number");
scanf("%s",s);
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]=='I')
a[i]=1;
else if(s[i]=='V')
a[i]=5;
else if(s[i]=='X')
a[i]=10;
else if(s[i]=='L')
a[i]=50;
else if(s[i]=='C')
a[i]=100;
else if(s[i]=='D')
a[i]=500;
else if(s[i]=='M')
a[i]=1000;
else
{
printf("Wrong Input");
getch();
exit(0);
}
}
40
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
k=a[l-1];
for(i=l-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("%d",k);
getch( );
}
Program 15:-
Aim: A Program to add and multiply two complex numbers (x+iy) and
(a+ib).
41
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<stdio.h>
#include<conio.h>
struct complex add(struct complex,struct complex);
struct complex mul(struct complex,struct complex);
struct complex
{
int real,imag;
};
void main( )
{
struct complex c1,c2,c3,c4;
printf("enter first number");
scanf("%d%d",&c1.real,&c1.imag);
printf("enter second number");
scanf("%d%d",&c2.real,&c2.imag);
c3=add(c1,c2);
c4=mul(c1,c2);
printf("addition is %d+i%d\n",c3.real,c3.imag);
printf("subtraction is %d+i%d",c4.real,c4.imag);
getch();
}
struct complex add(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return(c3);
}
struct complex mul(struct complex c1,struct complex c2)
{
42
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
c4.imag=(c1.imag*c2.real)+(c1.real*c2.imag);
return(c4);
}
Program 16(a):-
43
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Aim: A Program to copy contents of one file to another.
#include<stdio.h>
#include<conio.h>
void main( )
{
FILE *fs,*ft;
char ch;
fs=fopen(“file1.txt”,”r”);
if(fs==NULL)
{
printf(“cannot open file”);
exit(0);
}
ft=fopen(”file2.txt”,”w”);
if(ft==NULL)
{
printf(“cannot open file”);
exit(0);
}
while((ch=getc(fs))!=EOF)
{
putc(ch,ft);
}
fclose(ft);
printf(“ the copied contents are \n”);
ft=fopen(“file2.txt”, “r”);
while((ch=getc(ft))!=EOF)
{
putchar(ch);
}
fclose(fs);
fclose(ft);
getch( );
}
44
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output: the copied contents are
Marconi Institute of technology.
Program 16(b):-
#include<stdio.h>
#include<conio.h>
void main( )
{
FILE *fp;
char ch,str[10];
int i,n;
clrscr( );
fp=fopen(“file1.txt”,”r”);
if(fp==NULL)
{
printf(“file cannot be opened”);
exit(0);
}
printf(“enter n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
str[i]=getc(fp);
}
for(i=n-1;i>=0;i--)
{
putchar(str[i]);
}
while((ch=getc(fp))!=EOF)
{
putchar(ch);
45
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
getch( );
}
Output:enter n
4
college of engineering and technology
Program 17:-
#include<stdio.h>
#include<conio.h>
void create(int);
void insatend(int);
void insatpos(int,int);
void delatbeg( );
void delatend( );
void delatpos(int);
void traversal( );
struct node
{
int ele;
struct node *next;
};
struct node *start=NULL;
void main( )
{
int ch,ele;
printf(“1.create 2.insatend 3.insertion at position
46
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
4.delatbeg 5.delatend 6.deletion at position 7.traversal”);
do
{
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
create(ele);
break;
case 2: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
insatend(ele);
break;
47
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
getch( );
}
void create(int ele)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
temp->next=start;
start=temp;
}
}
void insatend(int ele)
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
while(p->next!=NULL)
p=p->next;
p->next=temp;
temp->next=NULL;
48
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
void insatpos(int ele,int pos)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
if(pos==1)
{
start=temp;
start->next=NULL;
}
else
printf(“insertion is not possible at this position”);
}
else if (start->next==NULL)
{
if(pos==1)
{
temp->next=start;
start->next=NULL;
start=temp;
}
else if(pos==2)
{
start->next=temp;
temp->next=NULL;
}
else
printf(“insertion is not possible at this position”);
}
else
{
49
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
int count=0;
struct node *p;
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count+1)
{
if(pos==1)
{
temp->next=start;
start=temp;
}
else
{
p=start;
for(i=1;i<pos-1;i++)
p=p->next;
temp->next=p->next;
p->next=temp;
}
}
else
printf(“insertion is not possible at this position”);
}
}
void delatbeg()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
50
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
temp=start;
start=NULL;
free(temp);
}
else
{
temp=start;
start=start->next;
free(temp);
}
}
void delatend()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
temp=start;
start=NULL;
free(temp);
}
else
{
struct node *p;
p=start;
while(p->next->next!=NULL)
p=p->next;
temp=p->next;
p->next=NULL;
free(temp);
}
}
void delatpos(int pos)
{
51
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
struct node *temp,*p;
int count=0;
if(start==NULL)
printf(“list is empty”);
else if (start->next==NULL)
{
if(pos==1)
{
temp=start;
start=NULL;
free(temp);
}
else
printf(“deletion is not possible at this position”);
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count)
{
if(pos==1)
{
temp=start;
start=start->next;
free(temp);
}
else
{
for(i=1;i<pos-1;i++)
p=p->next;
52
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
temp=p->next;
p->next=p->next->next;
free(temp);
}
}
else
printf(“deletion is not possible at this position”);
}
}
void traversal()
{
struct node*p;
if(start==NULL)
printf(“list is empty”);
else
{
p=start;
while(p!=NULL)
{
printf(“%d\t”,p->ele);
p=p->next;
}
}
}
Output:1.create
2.insatend
3.insertion at position
4.delatbeg
5.delatend
6.deletion at position
7.traversal
enter your choice 1
enter element to be inserted 11
53
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
enter your choice 3
enter element and position to be inserted 23 3
enter your choice 1
Program 18:-
#include<stdio.h>
#include<conio.h>
void create(int);
void insatend(int);
void insatpos(int,int);
void delatbeg();
void delatend();
void delatpos(int);
void traversal();
void traversalback();
struct node
{
int ele;
struct node *prev;
struct node *next;
};
struct node *start=NULL;
void main()
{
int ch,ele;
printf(“1.create 2.insatend 3.insertion at position
4.delatbeg 5.delatend 6.deletion at position 7.traversal
8.traversalback”);
54
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
do
{
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
create(ele);
break;
case 2: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
insatend(ele);
break;
case 3: printf(“enter element and position to be
inserted”);
scanf(“%d%d”,&ele,&pos);
insatpos(ele,pos);
break;
case 4: delatbeg();
break;
case 5: delatend();
break;
case 6: printf(“enter position to be deleted”);
scanf(“%d”,&pos);
delatpos(pos);
break;
case 7: traversal();
break;
case 8: traversalback();
break;
}
}
while(ch>=1&&ch<=8);
}
55
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void create(int ele)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->prev=NULL;
start->next=NULL;
}
else
{
temp->next=start;
temp->prev=NULL;
start=temp;
}
}
void insatend(int ele)
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->prev=NULL;
start->next=NULL;
}
else
{
p=start;
while(p->next!=NULL)
p=p->next;
temp->prev=p;
56
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
p->next=temp;
temp->next=NULL;
}
}
void insatpos(int ele,int pos)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
if(pos==1)
{
start=temp;
start->prev=NULL;
start->next=NULL;
}
else
printf(“insertion is not possible at this position”);
}
else if (start->next==NULL)
{
if(pos==1)
{
temp->next=start;
start->prev=temp;
start=temp;
start->prev=NULL;
}
else if(pos==2)
{
start->next=temp;
temp->prev=start;
temp->next=NULL;
}
57
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
else
printf(“insertion is not possible at this position”);
}
else
{
int count=0;
struct node *p;
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count+1)
{
if(pos==1)
{
temp->next=start;
start->prev=temp;
start=temp;
start->prev=NULL;
}
else
{
p=start;
for(i=1;i<pos-1;i++)
p=p->next;
temp->next=p->next;
p->next->prev=temp;
p->next=temp;
temp->prev=p;
}
}
else
printf(“insertion is not possible at this position”);
58
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
void delatbeg()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
temp=start;
start=NULL;
free(temp);
}
else
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
}
void delatend()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
temp=start;
start=NULL;
free(temp);
}
else
{
struct node *p;
59
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
p=start;
while(p->next->next!=NULL)
p=p->next;
temp=p->next;
p->next=NULL;
free(temp);
}
}
void delatpos(int pos)
{
struct node *temp,*p;
int count=0;
if(start==NULL)
printf(“list is empty”);
else if (start->next==NULL)
{
if(pos==1)
{
temp=start;
start=NULL;
free(temp);
}
else
printf(“deletion is not possible at this position”);
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count)
{
60
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(pos==1)
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
else
{
for(i=1;i<pos-1;i++)
p=p->next;
temp=p->next;
temp->next->prev=p;
p->next=p->next->next;
free(temp);
}
}
else
printf(“deletion is not possible at this position”);
}
}
void traversal()
{
struct node*p;
if(start==NULL)
printf(“list is empty”);
else
{
p=start;
while(p!=NULL)
{
printf(“%d\t”,p->ele);
p=p->next;
}
}
61
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
void traversalback()
{
struct node *p;
if(start==NULL)
printf(“list is empty”);
else
{
p=start;
while(p->next!=NULL)
p=p->next;
while(p!=NULL)
{
printf(“%d\t”,p->ele);
p=p->prev;
}
}
}
Output:1.create
2.insatend
3.insertion at position
4.delatbeg
5.delatend
6.deletion at position
7.traversal
8.traversalback
enter your choice 1
enter element to be inserted 11
enter your choice 3
enter element and position to be inserted 23 3
enter your choice 1
62
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 19:-
#define max 20
void push(int);
int pop();
void display();
int stack[max],top=-1;
void main()
{
int ch,ele;
printf(“1.Push\n2.Pop\n3.Display”);
do
{
printf(“Enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“Enter element to be inserted”);
scanf(“%d”,&ele);
63
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
push(ele);
break;
case 2: ele=pop();
if(ele!=-1)
printf(“Deleted element is %d”,ele);
break;
case 3: display();
break;
}
}while(ch>=1&&ch<=3);
getch();
}
void push(int ele)
{
if(top>=max-1)
printf(“stack is full”);
else
{
top++;
stack[top]=ele;
}
int pop()
{
int ele;
if(top==-1)
{
printf(“Stack is empty”);
return(-1);
}
else
{
ele=stack[top];
top--;
return(ele);
64
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
void display()
{
if(top==-1)
printf(“Stack is empty”);
else
{
for(i=0;i<=top;i++)
printf(“%d” stack[i]);
}
}
Output: 1.Push
2.Pop
3.Display
Enter ur choice
1
Enter element to be inserted
11
Enter ur choice
2
Deleted element is 11
Enter ur choice
3
Stack is empty
65
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 20:-
#include<stdio.h>
#include<conio.h>
void push( );
void pop( );
void display( );
struct node
{
int ele;
struct node *next;
}*start;
void main( )
66
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int ch;
start=NULL;
do
{
printf(“1.Push\n 2.Pop\n 3.Display\n”);
printf(“Enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: push() ;
break;
case 2: pop( );
break;
case 3: display( );
break;
default: printf(“Enter choice”);
break;
}
}
while(ch>=1&&ch<=3);
}
void push( )
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf(“Enter element to be inserted”);
scanf(“%d”,&temp->ele);
temp->next=start;
start=temp;
}
void pop( )
{
struct node *temp;
if(start==NULL)
67
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
printf(“list is empty”);
else
{
temp=start;
printf(“Deleted element is %d”,temp->ele);
start=start->next;
free(temp);
}
}
void display( )
{
struct node *p;
p=start;
if(start==NULL)
printf(“List is empty”);
else
{
while(p!=NULL)
{
printf(“%d”,p->ele);
p=p->next;
}
}
}
Output: 1.Push
2.Pop
3.Display
Enter ur choice
1
Enter element to be inserted
11
Enter ur choice
2
68
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Deleted element is 11
Enter ur choice
3
List is empty
Program 21:-
#define max 20
void enqueue(int);
int dequeue();
void display();
int queue[max],f=0,r=-1;
void main()
69
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int ele, ch;
do
{
printf(“1.Enqueue\n 2.Dequeue \n3.Display\n”);
printf(“Enter ur choice”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: printf(“Enter element to be inserted”);
scanf(“%d”,&ele);
enqueue(ele);
break;
case 2: ele=dequeue();
if(ele!=-1)
printf(“Deleted element is %d”,ele);
break;
case 3: display();
break;
}
}
while(ch>=1&&ch<=3);
}
void enqueue(int ele)
{
r++;
if(r>=max-1)
printf(“queue is full”);
else
{
queue[r]=ele;
}
}
int dequeue()
70
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int ele;
if(f>r)
{
printf(“queue is empty”);
}
else
{
ele=queue[f];
f++;
return(ele);
}
}
void display()
{
int i;
if(f>r)
{
printf(“queue is empty”);
}
else
{
for(i=f;i<=r;i++)
printf(“%d “,queue[i]);
}
}
Output:1.Enqueue
2.Dequeue
3.Display
Enter ur choice
1
Enter element to be inserted
11
71
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Enter ur choice
2
Deleted element is 11
Program 22:-
#include<stdio.h>
#include<conio.h>
void enqueue( );
void dequeue( );
void display( );
struct node
{
int ele;
struct node *next;
}*start;
void main()
{
int ch;
start=NULL;
do
{
printf(“1.Enqueue 2.Dequeue 3.Display\n”);
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: enqueue( );
break;
case 2: dequeue( );
break;
case 3: display( );
break;
72
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
default: printf(“Enter choice between 1 and 3”);
break;
}
}
while(ch>=1&&ch<=3);
getch( );
}
void enqueue()
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
printf(“Enter element to be inserted”);
scanf(“%d”,&temp->ele);
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
}
p->next=temp;
temp->next=NULL;
}
}
void dequeue()
{
struct node *temp;
if(start==NULL)
{
printf(“queue is empty”);
73
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
else
{
temp=start;
printf(“Deleted element is %d”,temp->ele);
start=start->next;
free(temp);
}
}
void display()
{
struct node *p;
p=start;
if(start==NULL)
{
printf(“queue is empty”);
}
else
{
while(p!=NULL)
{
printf(“%d”,p->ele);
p=p->next;
}
}
}
Output: Enqueue
Dequeue
Display
Enter ur choice
1
Enter element to be inserted
74
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
11
Enter ur choice
2
Deleted element is 11
Program 23:-
#include<stdio.h>
75
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<conio.h>
#include<ctype.h>
void push(char);
char pop( );
int opera(char);
int prio(char);
char stack[20];
int top=-1;
void main( )
{
char inf[10],ch;
int i;
printf(“enter infix expression”);
scanf(“%s”,inf);
for(i=0;inf[i]!=’\0’;i++)
{
if(isalnum(inf[i]))
printf(“%c”,inf[i]);
else if(opera(inf[i])
{
while(prio(inf[i])<=prio(stack[top]))
{
printf(“%c”,pop());
}
push(inf[i]);
}
else if(inf[i]==’(‘)
push(inf[i]);
else if(inf[i]==’)’)
{
while((ch=pop())!=’(‘)
{
printf(“%c”,ch);
}
}
76
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
while(top!=-1)
printf(“%c”,pop());
getch( );
}
int opera(char ch)
{
if(ch==’+’||ch==’-‘||ch==’*’||ch==’/’||ch==’%’)
return(1);
else
return(0);
}
int prio(char ch)
{
switch(ch)
{
case ‘+’:
case ‘-‘: return(1);
case ‘*’:
case ‘/’:
case ‘%’:return(2);
default: return(0);
}
}
void push(char ch)
{
if(top>=max-1)
{
printf(“stack is full”);
}
else
{
top++;
stack[top]=ch;
}
77
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
char pop( )
{
char ch;
if(top==-1)
{
printf(“stack is empty”);
exit(0);
}
else
{
ch=stack[top];
top--;
return(ch);
}
}
Program 24:-
78
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void push(int);
int pop();
int opera(char);
int stack[10],top=-1;
void main()
{
int i,x,y;
char post[10];
printf(“enter any postfix expression”);
scanf(“%s”,post);
for(i=0;post[i]!=’\0’;i++)
{
if(isdigit(post[i])
{
push(post[i]-48);
}
else if(opera(post[i])
{
x=pop();
y=pop();
switch(post[i])
{
case ‘+’: push(y+x);
break;
case ’-‘: push(y-x);
break;
case ’*’: push(y*x);
break;
case ‘/’: push(y/x);
break;
case ‘%’: push(y%x);
break;
}
}
}
79
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(top==0)
{
printf(“result=%d”,pop());
}
getch();
}
void push(int ele)
{
if(top>=max-1)
{
printf(“stack is full”);
}
else
{
top++;
stack[top]=ele;
}
}
int pop()
{
int ele;
if(top==-1)
{
printf(“stack is empty”);
return(-1);
}
else
{
ele=stack[top];
top--;
return(ele);
}
}
int opera(char ch)
{
80
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(ch==’+’||ch==’-‘||ch==’*’||ch==’/’||ch==’%’)
return(1);
else
return(0);
}
Input: enter any infix expression
12+3-
Output: 0
Program 25:-
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct bt
{
struct bt *lc;
int d;
struct bt *rc;
}node;
81
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
clrscr( );
printf("\n Enter the Number of Nodes \n");
scanf("%d",&n);
printf("\n Enter the Elements \n");
for(i=1;i<=n;i++)
{
scanf("%d",&e);
insert(&root,e);
}
inorder(root);
postorder(root);
preorder(root);
}
if(*r==NULL)
{
*r=(node *)malloc(sizeof(node));
(*r)->lc=(*r)->rc=NULL ;
(*r)->d=e;
return;
}
else
{
if(e<(*r)->d)
insert(&((*r)->lc),e);
else
insert(&((*r)->rc),e);
}
}
82
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
if(r!=NULL)
{
inorder(r->lc);
printf("Inorder elements are: %d\n",r->d);
inorder(r->rc);
}
else
return;
}
83
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
84
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 26:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],i,n,search;
printf(“Enter no of elements \n”);
scanf(“%d”,&n);
printf(“Enter elements \n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter element to be searched\n”);
scanf(“%d”,&search);
for(i=0;i<n;i++)
{
if(a[i]==search)
{
printf(“ %d is found at %d
position”,search,i+1);
exit();
}
}
printf(“element %d is not found”, search);
getch( );
}
Program 27:-
87
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Program 28:-
88
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output: Sorted order is
4 5 9
Program 29:-
#include<stdio.h>
#include<conio.h>
int partition(int m,int n);
void quick_sort(int p,int q);
int a[10];
void main( )
{
int i,size,p=0,q;
printf(“Enter size of array\n”);
scanf(“%d”,&size);
q=size-1;
printf(“Enter elements:”);
for(i=0;i,size;i++)
scanf(“%d”,&a[i]);
quick_sort(p,q);
printf(“After sorting “);
for(i=0;i,size;i++)
scanf(“%d ”,&a[i]);
getch( );
}
void quick_sort(int p,int q)
{
int j;
if(p<q)
{
j=partition(p,q+1);
quick_sort(p,j-1);
quick_sort(j+1,q);
}
89
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
int(partition(int m,int n)
{
int key,l,r,temp;
key=a[m];
l=m;
r=n;
do
{
do
{
l++;
}
while(a[l]<key);
do
{
r--;
}
while(a[r]>key);
if(l<r)
{
temp=a[l];
a[l]=a[r];
a[r]=temp;
}
}
while(l<r);
a[m]=a[r];
a[r]=key;
return( r);
}
90
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
5
Enter elements
7 2 9 1 3
Output: After sorting
1 2 3 7 9
Program 30:-
Aim: To write a program to implement Insertion sort.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],i,j,n,small;
clrscr( );
printf(“enter the size of an array \n”);
scanf(“%d”,&n);
printf(“enter elements :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
{
small=a[i];
j=i;
while((j>0)&&a[j-i]>small)
{
a[j]=a[j-i];
j--;
}
a[j]=small;
}
printf(“Sorted order is\n”);
for(i=0;i<n;i++)
printf(“%d ”,a[i]);
getch();
}
91
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Input: Enter size of an array
3
Enter elements
2 7 3
Output: Sorted array is
2 3 7
Program 31:-
{
c[k]=b[j];
j++;
}
k++;
}
while(i<n1)
{
c[k]=a[i];
i++;
K++;
}
while(j<n2)
{
c[k]=b[j];
j++;
k++;
}
*n=k;
}
Input: Enter size of first array
93
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
3
Enter elements of first array in sorted order
1 2 3
Enter size of second array
4
Enter elements of second array in sorted order
4 5 6 7
Output: after sorting
1 2 3 4 5 6 7
Program 32:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,m,j;
float x[10],y[10],t,sum,tk,k;
printf("enter number of values");
scanf("%d",&n);
printf("enter values for x and y");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
printf("enter the required interpolation value");
scanf("%f",&t);
sum=0;
for(k=0;k<n;k++)
{
tk=1;
for(j=0;j<n;j++)
{
94
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(j!=k)
{
tk=tk*((t-x[j])/(x[k]-x[j]));
}
}
sum=sum+(tk*y[k]);
}
printf("x=%f,y=%f",t,sum);
getch( );
}
Program 33:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,k,j;
float x[20],y[20],deltay[20]
[20],a,h,p,pvalue,factvalue,term,sumy=0;
printf("enter number of pairs\n");
scanf("%d",&n);
printf("enter values for x\n");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
printf("enter values for y\n");
for(i=0;i<n;i++)
95
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf("%f",&y[i]);
printf("enter the x value required\n");
scanf("%f",&a);
h=x[1]-x[0];
p=(a-x[0])/h;
for(i=0;i<n;i++)
deltay[i][0]=y[i];
for(i=0;i<n;i++)
deltay[i][1]=deltay[i+1][0]-deltay[i][0];
k=n-2;
for(i=2;i<n;i++)
{
k=k-1;
for(j=0;j<=k;j++)
deltay[j][i]=deltay[j+1][i-1]-deltay[j][i-1];
}
pvalue=1;
factvalue=1;
sumy=y[0];
for(i=1;i<n;i++)
{
pvalue=pvalue*(p-i+1);
factvalue=factvalue*i;
term=(pvalue*deltay[0][i]);
sumy=sumy+term;
}
printf("the interpolated value of %f is %f",a,sumy);
getch( );
}
96
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
0.3
0.4
enter values for y
1.005
1.020
1.045
1.081
enter the x value required
0.16
the interpolated value of 0.160000 is 1.011936
Program 34:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,j;
float x[20],y[20],sx=0,sy=0,sxy=0,a0,a1,sx2=0,mx,my;
clrscr( );
printf("how many sets of data you want to enter");
scanf("%d",&n);
printf("enter x,y values in sets\n");
for(i=0;i<n;i++)
scanf("%f%f",&x[i],&y[i]);
for(i=0;i<n;i++)
{
sx=sx+x[i];
sy=sy+y[i];
sxy=sxy+(x[i]*y[i]);
sx2=sx2+(x[i]*x[i]);
97
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
a0=(sy*sx2-sx*sxy)/(n*sx2-sx*sx);
a1=(n*sxy-sx*sy)/(n*sx2-sx*sx);
printf("equation is y=%f+%fx",a0,a1);
printf("enter for which vlaue u want to know y");
scanf("%f",&mx);
my=a0+a1*mx;
printf("%f",my);
getch( );
}
Program 35:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int n,i,j,k,m;
float x[20],y[20],c[20][20],u=0,a[20];
clrscr( );
printf("enter n,m\n");
scanf("%d%d",&n,&m);
printf("enter the data\n");
98
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&y[i]);
for(j=1;j<=m+1;j++)
{
for(k=1;k<=m+1;k++)
{
c[j][k]=0;
for(i=1;i<=n;i++)
{
c[j][k]=c[j][k]+pow(x[i],(j+k-2));
}
}
}
for(j=1;j<=m+1;j++)
{
c[j][m+2]=0;
for(i=1;i<=n;i++)
{
c[j][m+2]+=(y[i]*pow(x[i],(j-1)));
}
}
for(k=1;k<=m+1;k++)
{
for(i=1;i<=m+1;i++)
{
if(i!=k)
{
u=c[i][k]/c[k][k];
for(j=k;j<=m+2;j++)
c[i][j]-=(u*c[k][j]);
}
}
}
for(i=1;i<=m+1;i++)
a[i]=c[i][m+2]/c[i][i];
99
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i=1;i<=m+1;i++)
printf("a[%d]=%f\n",i-1,a[i]);
getch( );
}
Output: enter n, m
42
enter the data
24
3.5 3.8
5.2 5
66
a[0]=5.970579
a[1]=-1.486140
a[2]=0.248912
Program 36(a):-
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (1/(1+(x*x)))
void main( )
{
float a,b,sum=0,x,value,h;
int n,i;
clrscr( );
printf("enter a,b,n values\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
x=a;
sum=f(a);
for(i=1;i<n;i++)
{
100
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
x=x+h;
sum+=(2+f(x));
}
sum+=f(b);
value=(sum*h)/2;
printf("the value is %f",value);
getch( );
}
Program 36(b):-
102
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
103
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
ADDITIONAL EXPERIMENTS
Program 1:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,x,b;
float res;
printf(“enter values of a,x,b\n”);
104
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf(“%d%d%d”,&a,&x,&b);
res=(float)(a*x+b)/(a*x-b);
printf(“result=%f”,res);
getch( );
}
Program 2:-
#include<stdio.h>
#include<conio.h>
void main( )
{
int r,n,sum=0,tn;
printf(“enter any number\n”);
scanf(%d”,&n);
105
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
while(n>0)
{
r=n%10;
sum =sum+r*r*r;
n/=10;
}
if(tn==sum)
printf(“%d is armstrong”,sum);
else
printf(“%d is not Armstrong”,sum)
getch( );
}
Program 3:-
106
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(“Ascending order is\n”);
for(i=0;i<n;i++)
printf(“%d “,a[i]);
getch( );
}
Input:enter size of the array
4
enter elements
4321
Output:Ascending order is
1234
Program 4:-
#include<stdio.h>
#include<conio.h>
void sel_sort(int x[ ], int n);
void main( )
{
int x[10],i,j,n;
107
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
printf(“Enter the size of an array \n”);
scanf(“%d”,&n);
printf(“Enter elements :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&x[i]);
sel_sort(x,n);
printf(“Sorted order is \n”);
for(i=0;i<n;i++)
printf(“%d ”,x[i]);
getch( );
}
void sel_sort(int x[ ], int n)
{
int i,j,index,small;
for(i=0;i<n;i++)
{
small=x[i];
index=i;
for(j=i+1;j<n;j++)
{
if(x[j]<small)
{
small=x[j];
index=j;
}
}
x[index]=x[i];
x[i]=small;
}
}
108
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Input: Enter size of an array
5
Enter elements
2 7 3 9 1
Output: Sorted 0rder is
1 2 3 7 9
109
MARCONI INSTITUTE OF TECHNOLOGY