Sunteți pe pagina 1din 112

COMPUTER PROGRAMMING LAB

WEEK-1
A) Aim: A C program to find the sum of individual digits of a
positive integer.
Algorithm:
1. start
2. read num
3. while(num!=0)
{
3.1 rem=num10!
3.2 sum=sum"rem!
3.3 num=num#10!
$
%. write sum
&. end
Flowchart:
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
1
*top
*tart
+ead num
,um
-0
Compute rem=num10!
Compute sum=sum"rem
Compute num=num#10
.rite sum
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
void main()
{
int num1 2=11 sum=0!
)lrs)r()!
printf(34nter the num5er whose digits are to 5e added63)!
s)anf(3d317num)!
while(num!=0)
{
2=num10!
sum=sum"2!
2=num#10!
num=2!
$
printf(3*um of the digits6d31sum)!
get)h()!
$
Input & Output:
1.4nter the num5er whose digits are to 5e added 123
sum of the digits68
2.4nter the num5er whose digits are to 5e added 2&8
sum of the digits613
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
2
B) Aim: A C 9rogram to generation first , :i5ona))i series
Algorithm:
1. start
2. read no
3. assign num1;11 num21
%. for ) = 1 to no;2 in steps of 1
{
%.1 fi5=num1 " num2
%.2 write fi5
%.3 num1=num2
%.% num2=fi5!
$
&. end
Flowchart:
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
3
*tart
+ead no
Assign num1=;1 1num2=1
Compute fi5= num1"num2
Assign num1=num2
Assign nu2=fi5
.rite fi5
*top
C=1 C0=no
C""
Program code:
void main()
{
int num1=;11num2=11no1)ounter1fi5!
)lrs)r()!
printf(3<n<n<n<t<t4,(4+ =4,>(? @: *4+A4* (,) 63)!
s)anf(3d317no)!
printf(3<n<n<t<t<t0;;;;:AB@,ACCA *4+A4*;;;;-3)!
for()ounter = 1! )ounter 0= no! )ounter"")
{
fa5=num1 " num2!
printf(3 d31fi5)!
num1=num2!
num2=fi5!
$
$
Input & Output:
4,(4+ =4,>(? @: *4+A4* &
0 1 1 2 3
4,(4+ =4,>(? @: *4+A4* C
0 1 1 2 3 & C 13
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%
) AI! : A C program to generate all the prime num5ers 5etween 1 and
n1 where n is a value supplied 5D the user.
Algorithm:
1.. start
2. read ,
3. for() = 1! ) 0= ,! )"")
{
3.1 )he)2 = 0!
3.2 for()1 = );1! )1 - 1 ! )1;;)
3.2.1 if())1 == 0)
{
)he)2""!
5rea2!
$
3.2.2 if()he)2 == 0)
.rite )
$
%. end
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&
Flowchart:
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
8
*tart
+ead ,
Af(C
C1==0)
Af
()h==0)
.rite C
Compute )h=)h"1
C=1 C0=,
C""
C1=C;1 C1-1
C1;;
*top
Program :
/in)lude 0stdio.h-
/in)lude0)onio.h-
void main()
{
int no1)ounter1)ounter11)he)2!
)lrs)r()!
printf(3<n<n<n<t<t<tA,9E( (?4 FA=E4 @: ,6 3)!
s)anf(3d317no)!
printf(3<n<n(?4 9+A'4 ,@. *4+A4* B#. 1 (@ d 6 <n<n31no)!
for()ounter = 1! )ounter 0= no! )ounter"")
{
)he)2 = 0!
for()ounter1 = )ounter;1! )ounter1 - 1 ! )ounter1;;)
if()ounter)ounter1 == 0)
{
)he)2""!
5rea2!
$ if()he)2
== 0)
printf(3d<t31)ounter)!
$
get)h()!
$
Input & Output:
A,9E( (?4 FA=E4 @: ,6 &
1 2 3 & G
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G
COMPUTER PROGRAMMING LAB
WEEK-"
A) AI!: A C program to )al)ulate the following sum
*um= 1; H
2
#2! " H
%
#%! I H
8
#8! " H
C
#C! I H
10
#10! ; ; ;
Algorithm:
16 *tart
26 AnitialiJe varia5les i1 H1 n1 f1 sum 0
36 read the values for H and n
%6 for i=01 i0= n1 i""1 f K= i
&6 if i % = = 0
86 sum sum " pow (H1 i)#f!
G6 else if i 2 = =0
C6 sum sum I pow(H1 i)#f!
L6 print sum
106 *top
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
/in)lude0math.h-
void main( )
{
int i1 H1n!
long int f=1!
float sum=0!
)lrs)r ( )!
printf (M4nter the value for H and n <nN)!
s)anf (Md dN1 7H1 7n)!
for (i=0! i0=n! i""1 f K=i)
{
if ((i %) = =0)
sum"=pow(H1 i)#f!
else if ((i 2) = = 0)
sum;=pow(H1 i)#f!
$
printf (Msum of the series6 f <nN1 sum)!
get)h ( )!
$
Output:
4nter the value for H and n6 & &
*um of the series6 1%.&%
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C
B) AI!: A perfe)t num5er is a num5er that is the sum of all its divisors eH)ept itself.
*iH is the perfe)t num5er. (he onlD num5ers that divide 8 evenlD are 11213
and 8 = 1"2"3.
An a5undant num5er is one that is less than the sum of its divisors (eH6 12
01"2"3"%"8).
A defi)ient num5er is greater than the sum of its divisors( eH6 L - 1"3).
.rite a program to generate a ta5le of the first , integers (+ead , from
2eD5oard) and )lassifD ea)h as perfe)t1 a5undant or defi)ient.
Algorithm:
1.*tart
2.+ead a num5er1 n
3.for i=1 to n#2 in steps of n
if( ni==0)
sum=sum"i!
endif
%.endfor
&.if( sum==n)
.rite M perfe)t num5erN
8.else if(sum0n)
.rite Ma5undant num5erN
G.else
.rite Mdefi)ient num5erN
C.endif
L.*top.
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
void main() {
int n1m1sum=01i!
)lrs)r()!
printf(34nter a num5er663)!
s)anf(3d317n)!
for(i=1!i0=n#2!i"")
if(ni==0)
sum=sum"i!
if(sum==n)
printf(3perfe)t num5er3)!
else if(sum0=m)
printf(3a5undant num5er3)!
else
printf(3Oefi)ient num5er3)!
get)h()!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L

Input & Output:

1.4nter a num5er668
perfe)t num5er

2.4nter a num5er6612
a5undant num5er
3.4nter a num5er66L
Oefi)ient num5er
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
10
COMPUTER PROGRAMMING LAB
WEEK-#
A) AI!6 C 9rogram to find 5oth the largest and smallest num5er in a list of integers

Algorithm:
16 *tart
26 initialiJe varia5les
36 +ead values of varia5les
%6 +ead n and arraD element
&6 for i0 to n;1 is steps of 3 do
86 read aPiQ
=et maH min a P0Q
G6 for i 1 to n;1 is step of 3 do
C6 print maH min aPiQ
L6 *top
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
void main ( )
{
int aP10Q1n1 i1 large1 small!
)lrs)r( )!
printf(M4nter num5er of elements <nN)!
s)anf(MdN17n)!
printf(M4nter num5er of arraD elements <nN)!
for(i=0!i0n!i"")
s)anf(MdN17aPiQ)!
large=small=a P0Q!
for(i=1!i0n!i"")
{
if(large 0 aPiQ)
large=aPiQ!
if(small - aPiQ)
small=aPiQ!
$
printf(M(he =argest ,um5er is d <nN1 large)!
printf(M(he *mallest ,um5er is d<nN1 small)!
get)h( )!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
11
Input & Output:
4nter num5er of element6 10
4nter arraD elements6 12 3 8 G 2 10 % & C L

(he =argest ,um5er is 12
(he *mallest ,um5er is 2
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
12
B) AI!: C 9rogram that uses fun)tion to perform the following6
i) Addition of two matri)es.
ii) 'ultipli)ation of two matri)es.
i$ program to per%orm addition o% two matrice&$
Algorithm:
16 *tart
26 +ead r11 r21 )11 )2
36 if r1==r2 77 )1==)2
%6 +ead elements in first matriH m1PiQPRQ
&6 +ead elements in to se)ond matriH m2PiQPRQ
86 adding two matri)es m1PiQPRQ"m2PiQPRQ
G6 print resultant matriH m3PiQPRQ
C6 else write 'atriH Addition is not possi5le
L6 *top

Program:
/in)lude0stdio.h-
/in)lude0)onio.h-
void main( )
{
int m1P3QP3Q1 m2P3QP3Q1 m3P3QP3Q!
int r11)11r21)21i1R!
)lrs)r()!
printf(M4nter order of first matriH <nN)!
s)anf (MddN17r117)1)!
printf(M4nter order of se)ond matriH <nN)!
s)anf(MddN17r217)2)!
if((r1==r2) 77 ()1==)2))
{
printf(M4nter elements in to first matriH <nN)!
for(i=0!i0r1!i"")
for(R=o!R0)1!R"")
s)anf(MdN17m1PiQPRQ)!
printf(M4nter elements in to se)ond matriH <nN)!
for(i=0!i0r2!i"")
for(R=o!R0)2!R"")
s)anf(MdN17m2PiQPRQ)!
printf(M+esultant 'atriH <nN)!
for(i=0!i0r1!i"")
{
for(R=0!R0)1!R"")
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
13
{
m3PiQPRQ=m1PiQPRQ " m2PiQPRQ
printf(M%dN1m3PiQPRQ)!
$
printf(M<nN)!
$
$
else
printf(M'atriH Addition As ,ot 9ossi5leN)!
get)h()!
$
Input & Output:
4nter order of first matriH6
3
3
4nter order of se)ond matriH6
3
3
4nter elements in to first matriH
2 3 %
% & 8
8 G C
4nter elements in to se)ond matriH
1 1 1
2 2 2
3 3 3
+esultant 'atriH
3 % &
8 G C
L 10 11
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
1%
ii$ program to per%orm matri' multiplication
Algorithm:
16 start
26 read r11)11r21)2
36 if ()1==r2)
for(i=0!10r1!i"")
for(R=0!R0)1!R"")
%6 read m1(313)
&.end for
86 for(i0! i0r2! ii"1)
for (R0 !R0)2! R "")
read m2(313)
G.end for
C6 for (i 0!i0r1!ii"1)
for(R0!R0)2!RR"1)
L6 m3PiQPRQ0
106 for (2 0! 20r2!22"1)
116 m3PiQPRQ m3 PiQPRQ"m1PRQP2Q"m2P2QPRQ
write m3PiQPRQ
126 else
matriH multipli)ation is not possi5le
136 stop
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
void main( )
{
int m1P3QP3Q1m2P3QP3Q1m3P3QP3Q!
int r11)11r21)21A1R12!
)lrs)r( )!
printf(Menter order of first matriH <nN)!
s)anf(MddN17r117)1)!
printf(Menter order of se)ond matriH <nN)!
s)anf(MddN17r217)2)!
if()1==r2)
{
printf(Menter a element into first matriH <nN)!
for(i=0!i0r1!i"")
for (R=0!R0)1!R"")
s)anf(MdN17m1PiQPRQ)!
printf(Menter a element into se)ond matriH<n)!
for(i=0!A0r2!i"")
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
1&
for(R=0!R0)2!R"")
s)anf(MdN17m2PiQPRQ)!
printf(Mresultant matriH <nN)!
for(i=0!i0r1!i"")
{
for(R=0!R0)2!R"")
{
m3PiQPRQ=0!
for(2=0!20r2!2"")
m3PiQPRQ"=m1PiQP2QKm2P2QPRQ!
printf(M%dN1m3PiQPRQ)!
$
printf(M<nN)!
$
get)h( )!
$
$
Input & Output:
4nter order of first matriH
3 3
4nter order of se)ond matriH
3 3
4nter element into first matriH
1 1 1
2 2 2
3 3 3
4nter element into se)ond matriH
1 2 3
2 3 %
3 % &
+esultant matriH
8 L 12
12 1C 2%
1C 2G 38
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
18
COMPUTER PROGRAMMING LAB
WEEK-(
)A) AI!: .rite a program to perform the following6
i) =inear sear)h ii) BinarD sear)h
i) *inear &earch
Algorithm:
1.*et i to 1.
2.+epeat this loop6
3.Af i - n1 then eHit the loop.
Af APiQ = x1 then eHit the loop.
*et i to i " 1.
%. +eturn i.

Program code:
/in)lude 0stdio.h-
/define 'AST=4, 10
void lTsear)hTre)ursive(int lPQ1int num1int ele)!
void lTsear)h(int lPQ1int num1int ele)!
void readTlist(int lPQ1int num)!
void printTlist(int lPQ1int num)!
void main()
{
int lP'AST=4,Q1 num1 ele!
int )h!
)lrs)r()!
printf(3===============================================
=======3)!
printf(3<n<t<t<t'4,E3)!
printf(3<n==============================================
=======3)!
printf(3<nP1Q =inarD *ear)h using +e)ursion method3)!
printf(3<nP2Q =inarD *ear)h using ,on;+e)ursion method3)!
printf(3<n<n4nter Dour Choi)e63)!
s)anf(3d317)h)!
if()h0=2 7 )h-0)
{
printf(34nter the num5er of elements 63)!
s)anf(3d317num)!
readTlist(l1num)!
printf(3<n4lements present in the list are6<n<n3)!
printTlist(l1num)!

printf(3<n<n4lement Dou want to sear)h6<n<n3)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
1G
s)anf(3d317ele)!
swit)h()h)
{
)ase 16
printf(3<nKK+e)ursion methodKK<n3)!
lTsear)hTre)ursive(l1num1ele)!
get)h()!
5rea2!
)ase 26
printf(3<nKK,on;+e)ursion methodKK<n3)!
lTsear)hTnonre)ursive(l1num1ele)!
get)h()!
5rea2!
$
$
get)h()!
$
#K ,on;+e)ursive methodK#
void lTsear)hTnonre)ursive(int lPQ1int num1int ele)
{
int R1 f=0!
for(R=0!R0num!R"")
if( lPRQ == ele)
{
printf(3<n(he element d is present at position d in list<n31ele1R)!
f=1!
5rea2!
$
if(f==0)
printf(3<n(he element is d is not present in the list<n31ele)!
$
#K +e)ursive methodK#
void lTsear)hTre)ursive(int lPQ1int num1int ele)
{
int f = 0!
if( lPnumQ == ele)
{
printf(3<n(he element d is present at position d in list<n31ele1num)!
f=1!
$
else

{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
1C
if((num==0) 77 (f==0))
{
printf(3(he element d is not found.31ele)!
$
else
{
lTsear)h(l1num;11ele)!
$
$
get)h()!
$
void readTlist(int lPQ1int num)
{
int R!
printf(3<n4nter the elements6<n3)!
for(R=0!R0num!R"")
s)anf(3d317lPRQ)!
$
void printTlist(int lPQ1int num)
{
int R!
for(R=0!R0num!R"")
printf(3d<t31lPRQ)!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
1L
Input & Output:
!E+,
P1Q =inear sear)h using re)ursion method.
P2Q =inear sear)h using non re)ursion method
4nter Dour Choi)e61
4nter the num5er of elements 6G
4lements present in the list are6 3 2 1 & 8 G L
4lement Dou want to sear)h6&
(he element & is present at position % in list.
4nter Dour Choi)e62
4nter the num5er of elements 6&
4lements present in the list are6 2 1 & G L
4lement Dou want to sear)h6&
(he element & is present at position 3 in list.
4nter Dour Choi)e61
4nter the num5er of elements 6G
4lements present in the list are6 3 2 1 & 8 G L
4lement Dou want to sear)h610
(he element 10 is not present in list.
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
20
ii) Binar- &earch
Algorithm:
,+BTsear)h()
{
min 6= 1!
maH 6= ,! {arraD siJe6 var A 6 arraD P1..,Q of integer$
repeat
mid 6= (min " maH) div 2!
if H - APmidQ then
min 6= mid " 1
else
maH 6= mid ; 1!
until (APmidQ = H) or (min - maH)!
$
+BinarD*ear)h(AP0..,;1Q1 value1 low1 high)
{
if (high 0 low)
return ;1 ## not found
mid = low " ((high ; low) # 2)
if (APmidQ - value)
return BinarD*ear)h(A1 value1 low1 mid;1)
else if (APmidQ 0 value)
return BinarD*ear)h(A1 value1 mid"11 high)
else
return mid ## found
$

Program code:
in)lude 0stdio.h-
/define 'AST=4, 10
#K ,on;+e)ursive fun)tionK#
void 5Tsear)hTnonre)ursive(int lPQ1int num1int ele)
{
int l11i1R1 flag = 0!
l1 = 0!
i = num;1!
while(l1 0= i)
{
R = (l1"i)#2!
if( lPRQ == ele)
{
printf(3<n(he element d is present at position d in list<n31ele1R)!
flag =1!
5rea2!

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
21
$
else
if(lPRQ 0 ele)
l1 = R"1!
else
i = R;1!
$
if( flag == 0)
printf(3<n(he element d is not present in the list<n31ele)!
$
#K +e)ursive fun)tionK#
int 5Tsear)hTre)ursive(int lPQ1int arraD*tart1int arraD4nd1int a)
{
int m1pos!
if (arraD*tart0=arraD4nd)
{
m=(arraD*tart"arraD4nd)#2!
if (lPmQ==a)
return m!
else if (a0lPmQ)
return 5Tsear)hTre)ursive(l1arraD*tart1m;11a)!
else
return 5Tsear)hTre)ursive(l1m"11arraD4nd1a)!
$
return ;1!
$
void readTlist(int lPQ1int n)
{
int i!
printf(3<n4nter the elements6<n3)!
for(i=0!i0n!i"")
s)anf(3d317lPiQ)!
$
void printTlist(int lPQ1int n)
{
int i!
for(i=0!i0n!i"")
printf(3d<t31lPiQ)!
$
#Kmain fun)tionK#
void main()
{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
22
int lP'AST=4,Q1 num1 ele1f1l11a!
int )h1pos!
)lrs)r()!
printf(3======================================================3)!
printf(3<n<t<t<t'4,E3)!
printf(3<n=====================================================3)!
printf(3<nP1Q BinarD *ear)h using +e)ursion method3)!
printf(3<nP2Q BinarD *ear)h using ,on;+e)ursion method3)!
printf(3<n<n4nter Dour Choi)e63)!
s)anf(3d317)h)!
if()h0=2 7 )h-0)
{
printf(3<n4nter the num5er of elements 6 3)!
s)anf(3d317num)!
readTlist(l1num)!
printf(3<n4lements present in the list are6<n<n3)!
printTlist(l1num)!
printf(3<n<n4nter the element Dou want to sear)h6<n<n3)!
s)anf(3d317ele)!
swit)h()h)
{
)ase 16printf(3<n+e)ursive method6<n3)!
pos=5Tsear)hTre)ursive(l101num1ele)!
if(pos==;1)
{
printf(34lement is not found3)!
$
else
{
printf(34lement is found at d position31pos)!
$
get)h()!
5rea2!
)ase 26printf(3<n,on;+e)ursive method6<n3)!
5Tsear)hTnonre)ursive(l1num1ele)!
get)h()!
5rea2!
$
$
get)h()!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
23
Input & Output:
!E+,
P1Q BinarD sear)h using re)ursion method.
P2Q BinarD sear)h using non re)ursion method
4nter Dour Choi)e61
4nter the num5er of elements 6G
4lements present in the list are6 3 2 1 & 8 G L
4lement Dou want to sear)h6&
(he element & is present at position % in list.
4nter Dour Choi)e62
4nter the num5er of elements 6&
4lements present in the list are6 2 1 & G L
4lement Dou want to sear)h6&
(he element & is present at position 3 in list.
4nter Dour Choi)e61
4nter the num5er of elements 6G
4lements present in the list are6 3 2 1 & 8 G L
4lement Dou want to sear)h610
(he element 10 is not present in list.
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
2%
COMPUTER PROGRAMMING LAB
WEEK-.
AI!: .rite a program that implements the following sorting methods to sort a given list
of integers in as)ending order6
i) Bu55le sort ii) *ele)tion sort iii) Ansertion sort
i$ Bu//le &ort
Algorithm:
do
swapped 6= false
for ea)h i in 0 to length(A) ; 2 in)lusive do6
if APiQ - APi"1Q then
swap( APiQ1 APi"1Q )
swapped 6= true
end if
end for
while swapped
end pro)edure

Program code:
/in)lude 0stdio.h-
/define 'AS 10
void swap=ist(int Km1int Kn)
{
int temp!
temp = Km!
Km = Kn!
Kn = temp!
$
void 5u5Tsort(int listPQ1 int n)
{
int i1R!
for(i=0!i0(n;1)!i"")
for(R=0!R0(n;(i"1))!R"")
if(listPRQ - listPR"1Q)
swap=ist(7listPRQ17listPR"1Q)!
$
void readlist(int listPQ1int n)
{

int R!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
2&
printf(3<n4nter the elements6 <n3)!
for(R=0!R0n!R"")
s)anf(3d317listPRQ)!
$
#K *howing the )ontents of the list K#
void printlist(int listPQ1int n)
{
int R!
for(R=0!R0n!R"")
printf(3d<t31listPRQ)!
$
void main()
{
int listP'ASQ1 num!
)lrs)r()!
printf(3<n<n<nKKKKK 4nter the num5er of elements P'aHimum 10Q KKKKK<n3)!
s)anf(3d317num)!
readlist(list1num)!
printf(3<n<n4lements in the list 5efore sorting are6<n3)!
printlist(list1num)!
5u5Tsort(list1num)!
printf(3<n<n4lements in the list after sorting are6<n3)!
printlist(list1num)!
get)h()!
$
Input & Output 6
4nter the num5er of elementsP'aHimum 10Q 6 %
4lements in the list 5efore sorting are 6 3 2 1 %
4lements in the list 5efore sorting are 6 1 2 3 %
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
28
ii) 0election &ort$
Algorithm:
void sele)tion*ort(intPQ a)
{
for (int i = 0! i 0 a.length ; 1! i"")
{
int min = i!
for (int R = i " 1! R 0 a.length! R"")
{
if (aPRQ 0 aPminQ)
{
min = R!
$
$
if (i != min)
{
int swap = aPiQ!
aPiQ = aPminQ!
aPminQ = swap!
$
$
$

Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
void instTsort(int PQ)!
void main()
{
int numP&Q1)ount!
)lrs)r()!
printf(3<n4nter the :ive 4lements to sort6<n3)!
for ()ount=0!)ount0&!)ount"")
s)anf(3d317numP)ountQ)!
instTsort(num)!
printf(3<n<n4lements after sorting6 <n3)!
for()ount=0!)ount0&!)ount"")
printf(3d<n31numP)ountQ)!
get)h()!
$
## :un)tion for Ansertion *orting
void instTsort(int numPQ)
{
int i1R12!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
2G
for(R=1!R0&!R"")
{
2=numPRQ!
for(i=R;1!i-=0 77 20numPiQ!i;;)
numPi"1Q=numPiQ!
numPi"1Q=2!
$
Input & Output 6
4nter the num5er of elementsP'aHimum 10Q 6 %
4lements in the list 5efore sorting are 6 3 2 1 %
4lements in the list 5efore sorting are 6 1 2 3 %
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
2C
COMPUTER PROGRAMMING LAB
WEEK-11
A)AI! 6 .rite a C program that uses fun)tions to perform the following operations6
1.(o insert a su5;string in to given main string from a given position
Algorithm:
1. start
2. read str11 str21 str3
3. )all su5string(str11str21str3)
%. end
1. start
2. *u5string ()har string11 )har string21 )har string3)
{
r = strlen(string1)!
n = strlen(string2)!
i=0!
while(i 0= r)
{
*tring3PiQ=string1PiQ!
i""!
$
s = n"r!
o = p"n!
for(i=p!i0s!i"")
{
H = string3PiQ!
if(t0n)
{
*tring1PiQ = string2PtQ!
t=t"1!
$
*tring1PoQ=H!
o=o"1!
$
return string1
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
2L
Program ode:
/in)lude 0stdio.h-
/in)lude 0)onio.h-
/in)lude 0string.h-
void main()
{
)har aP10Q!
)har 5P10Q!
)har )P10Q!
int p=01r=01i=0!
int t=0!
int H1g1s1n1o!
)lrs)r()!
puts(34nter :irst *tring63)!
gets(a)!
puts(34nter *e)ond *tring63)!
gets(5)!
printf(34nter the position where the item has to 5e
inserted6 3)!
s)anf(3d317p)!
r = strlen(a)!
n = strlen(5)!
i=0!
while(i 0= r)
{
)PiQ=aPiQ!
i""!
$
s = n"r!
o = p"n!
for(i=p!i0s!i"")
{
H = )PiQ!
if(t0n)
{
aPiQ = 5PtQ!
t=t"1!
$
aPoQ=H!
o=o"1!
$
printf(3,ew string is6s31 a)!
get)h()!
$

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
30
Input: happD new Dear 2011

Output6 happD new 2011 Dear

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
31
B)AI! 6 .rite a C program that uses fun)tions to perform the following operations6
(o delete n Chara)ters from a given position in a given string.
Algorithm:
1. start
2. read str1 a1 5
3. )all del)har(str 1 a1 5)
%. end
1. strat
2. del)har()har Kstring 1int a1 int 5)
{
if ((a"5;1) 0= strlen(string))
{
str)pD(7HP5;1Q17HPa"5;1Q)!
return (string)!
$
$
3. end
Program code:
/in)lude 0stdio.h-
/in)lude 0)onio.h-
/in)lude 0string.h-
void del)har()har K1int 1 int )!
void main()
{
)har stringP10Q!
int n1pos1p!
)lrs)r()!
puts(34nter the string3)!
gets(string)!
printf(34nter the position from where to delete3)!
s)anf(3d317pos)!
printf(34nter the num5er of )hara)ters to 5e deleted3)!
s)anf(3d317n)!
del)har(string1 n1pos)!
get)h()!
$
del)har()har Kstring 1int a1 int 5)
{
if ((a"5;1) 0= strlen(string))
{

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
32
str)pD(7HP5;1Q17HPa"5;1Q)!
return (string)!
$



Input: 4nter the string 6FAOUA,AV4(?A,
4nter the position from where to delete 6&
4nter the num5er of )hara)ters to 5e deleted6 C
Output6 vidDa
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
33
COMPUTER PROGRAMMING LAB
WEEK-1"
A)AI! 6 .rite a C program that displaDs the position or indeH in the string * where the
string ( 5egins1 or ; 1 if * doesnWt )ontain (
Algorithm:
1. start
2. read s1 t
3. found=)all stastr(s1t)
%. if(found)
write3*e)ond *tring is found in the :irst *tring
else
write 3;13
&. end
Program code:
/in)lude0stdio.h-
/in)lude0string.h-
/in)lude0)onio.h-
void main()
{
)har sP30Q1 tP20Q!
)har Kfound!
)lrs)r()!
puts(34nter the first string6 3)!
gets(s)!
puts(34nter the string to 5e sear)hed6 3)!
gets(t)!
found=strstr(s1t)!
if(found)
printf(3*e)ond *tring is found in the :irst *tring at d
position.<n31found;s)!
else
printf(3;13)!
get)h()!
$
Input: 4nter the first string6 sree vidDani2ethan
4nter the string to 5e sear)hed6vidDa

Output6 *e)ond string is found in the :irst *tring at & position.
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
3%
B) AI! 6 C program to )ount the lines1 words and )hara)ters in a given teHt
Algorithm:
1. start
2. Countlw)()har )tr)
{
while(()tr=get)har()) != W<nW)
lineP)""Q = )tr!
lineP)Q = W<0W!
if(lineP0Q == W<0W)
5rea2 !
else
{
words""!
for(i=0! linePiQ != W<0W!i"")
if(linePiQ == W W XX linePiQ == W<tW)
words""!
$
lines = lines "1!
)hara)ters = )hara)ters " strlen(line)!
$
$
3. end
Program code:
/in)lude 0stdio.h-
main()
{
)har linePC1Q1 )tr!
int i1)1 end = 01)hara)ters = 01words = 01lines = 0!
printf(3V4U A, (?4 (4S(.<n3)!
printf(3>AF4 @,4 *9AC4 A:(4+ 4AC? .@+O.<n3)!
printf(3.?4, C@'9=4(4O1 9+4** W+4(E+,W.<n<n3)!
while( end == 0)
{
#K +eading a line of teHt K#
) = 0!
while(()tr=get)har()) != W<nW)
lineP)""Q = )tr!
lineP)Q = W<0W!
#K )ounting the words in a line K#
if(lineP0Q == W<0W)
5rea2 !
else
{
words""!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
3&
for(i=0! linePiQ != W<0W!i"")
if(linePiQ == W W XX linePiQ == W<tW)
words""!
$
#K )ounting lines and )hara)ters K#
lines = lines "1!
)hara)ters = )hara)ters " strlen(line)!
$
printf (3<n3)!
printf(3,um5er of lines = d<n31 lines)!
printf(3,um5er of words = d<n31 words)!
printf(3,um5er of )hara)ters = d<n31 )hara)ters)!
$


Input: V4U A, (?4 (4S(.
>AF4 @,4 *9AC4 A:(4+ 4AC? .@+O.
.?4, C@'9=4(4O1 9+4** W+4(E+,W.
Admiration is a verD short;lived passion.
Admiration involves a glorious o5liYuitD of vision.
AlwaDs we li2e those who admire us 5ut we do not
li2e those whom we admire.
:ools admire1 5ut men of sense approve.

Output6 ,um5er of lines = &
,um5er of words = 38
,um5er of )hara)ters = 20&
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
38
COMPUTER PROGRAMMING LAB
WEEK-1#
A) AI!6 C program to find the 2Zs )omplement of a 5inarD num5er.
Algorithm:
1. start
2. read a
3 int l1 i1 )=0!
%. l=strlen(a)!
&. for (i=l;1! i-=0! i;;)
{
if (aPiQ==W0W77 )=0)
)ontinue!
else if(aPiQ==Z1Z 77 )=0)
{
)=1!
)ontinue!
$
else
{
if(aPiQ==Z0Z)
aPiQ=Z1Z!
else
aPiQ=Z0Z!
$
$

1. write a
G. end
Program code :
/in)lude 0stdio.h-
/in)lude0)onio.h-
void )omplement ()har Ka)!
void main()
{
)har aP18Q!
int i!
)lrs)r()!
printf(34nter the 5inarD num5er3)!
gets(a)!
for(i=0!aPiQ!=W<0W! i"")

{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
3G
if (aPiQ!=W0W 77 aPiQ!=W1W)
{
printf(3(he num5er entered is not a 5inarD num5er. 4nter
the )orre)t num5er3)!
eHit(0)!
$
$
)omplement(a)!
get)h()!
$
void )omplement ()har Ka)
{
int l1 i1 )=0!
)har 5P18Q!
l=strlen(a)!
for (i=l;1! i-=0! i;;)
{
if (aPiQ==W0W77 )=0)
)ontinue!
else if(aPiQ==Z1Z 77 )=0)
{
)=1!
)ontinue!
$
else
{
if(aPiQ==Z0Z)
aPiQ=Z1Z!
else
aPiQ=Z0Z!
$
$
printf(3(he 2Ws )omplement is s31 a)!
$

Input: 4nter the 5inarD num5er 6 10101010110

Output: (he 2Ws )omplement is 6 01010101010
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
3C
B) AI! 6 C program to )onvert a +oman numeral to its de)imal eYuivalent.
Algorithm:
1. start
2. read a
3. len=strlen(rom)!
%.for(i=0!i0len!i"")
{
if(romPiQ==WAW)
aPiQ=1!
else if(romPiQ==WFW)
aPiQ=&!
else if(romPiQ==WSW)
aPiQ=10!
else if(romPiQ==W=W)
aPiQ=&0!
else if(romPiQ==WCW)
aPiQ=100!
else if(romPiQ==WOW)
aPiQ=&00!
else if(romPiQ==W'W)
aPiQ=1000!
else
{
.rite 3Anvalid Falue3!
eHit(0)!
$
$
2=aPlen;1Q!
for(i=len;1!i-0!i;;)
{
if(aPiQ-aPi;1Q)
2=2;aPi;1Q!
else if(aPiQ==aPi;1Q XX aPiQ0aPi;1Q)
2=2"aPi;1Q!
$
G..riteNAts Oe)imal 4Yuivalent is6312!
C. end
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
3L
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
/in)lude0string.h-
/in)lude0stdli5.h-
void main()
{
int Ka1len1i1R12!
)har Krom!
)lrs)r()!
printf(34nter the +oman ,umeral63)!
s)anf(3s31rom)!
len=strlen(rom)!
for(i=0!i0len!i"")
{
if(romPiQ==WAW)
aPiQ=1!
else if(romPiQ==WFW)
aPiQ=&!
else if(romPiQ==WSW)
aPiQ=10!
else if(romPiQ==W=W)
aPiQ=&0!
else if(romPiQ==WCW)
aPiQ=100!
else if(romPiQ==WOW)
aPiQ=&00!
else if(romPiQ==W'W)
aPiQ=1000!
else
{
printf(3<nAnvalid Falue3)!
get)h()!
eHit(0)!
$
$
2=aPlen;1Q!
for(i=len;1!i-0!i;;)
{
if(aPiQ-aPi;1Q)
2=2;aPi;1Q!
else if(aPiQ==aPi;1Q XX aPiQ0aPi;1Q)
2=2"aPi;1Q!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%0
printf(3<nAts Oe)imal 4Yuivalent is63)!
printf(3d312)!
get)h()!
$

Input 6 4nter the +oman numeral6 SAA

Output : Ats Oe)imal 4Yuivalent is6 12
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%1
COMPUTER PROGRAMMING LAB
WEEK-1(
A) AI!6 C programs that use re)ursive fun)tions to find the fa)torial of a
given integer.
Algorithm:
1. start
2. read n
3. f(n==0)
printf(3:a)torial of 0 is 1<n3)!
else
printf(3d<n31n1re)rTfa)torial(n))!

%. end
Program ode:
/in)lude0stdio.h-
/in)lude0)onio.h-
unsigned int re)rTfa)torial(int n)!
void main()
{
int n1i!
long fa)t!
)lrs)r()!
printf(34nter the num5er6 3)!
s)anf(3d317n)!
if(n==0)
printf(3:a)torial of 0 is 1<n3)!
else
{
printf(3d<n31n1re)rTfa)torial(n))!
$
get)h()!
$
unsigned int re)rTfa)torial(int n)
{
return n-=1 [ n K re)rTfa)torial(n;1) 6 1!
$
Input: Enter the num/er &
Output: 120
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%2
AI! 6 C programs that use ,on;re)ursive fun)tions to find the fa)torial of a
given integer.
Algorithm:
1. start
2. read n
3. f(n==0)
printf(3:a)torial of 0 is 1<n3)!
else
printf(3d<n31n1iterTfa)torial(n))!

%. end
Program ode:
/in)lude0stdio.h-
/in)lude0)onio.h-
unsigned int re)rTfa)torial(int n)!
void main()
{
int n1i!
long fa)t!
)lrs)r()!
printf(34nter the num5er6 3)!
s)anf(3d317n)!
if(n==0)
printf(3:a)torial of 0 is 1<n3)!
else
{
printf(3d<n31n1iterTfa)torial(n))!
$
get)h()!
$
unsigned int iterTfa)torial(int n)
{
int a))u = 1!
int i!
for(i = 1! i 0= n! i"")
{
a))u K= i!
$
return a))u!
$
Input: Enter the num/er &
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%3
Output: 120
AI! 6 C programs that use +e)ursive fun)tions to find the >CO
(greatest )ommon divisor) of two given integers.
Algorithm:
1. start
2. read a1 5
3. write a151>)d+e)ursive(a15))!
%. end
1. start
2. if(n-m)
return >)d+e)ursive(n1m)!
if(n==0)
return m!
else
return >)d+e)ursive(n1mn)!
3. end
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
/in)lude0math.h-
unsigned int >)d+e)ursive(unsigned m1 unsigned n)!
int main(void)
{
int a151i>)d!
)lrs)r()!
printf(34nter the two num5ers whose >CO is to 5e found6 3)!
s)anf(3dd317a175)!
printf(3ddd<n31a151>)d+e)ursive(a15))!
get)h()!
$
unsigned int >)d+e)ursive(unsigned m1 unsigned n)
{
if(n-m)
return >)d+e)ursive(n1m)!
if(n==0)
return m!
else
return >)d+e)ursive(n1mn)!
$
Input: 4nter the two num5er whose >CO is to 5e found6 & 10
Output: &
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%%
B) A C program that use ,on;re)ursive fun)tions to find the >CO
(greatest )ommon divisor) of two given integers.
Algorithm:
1. start
2. read a1 5
3. write a151>)d,on+e)ursive(a151n))!
% . end
>)d,on+e)ursive(p1Y1n)
if(n==0)
return m!
else
unsigned remainder!
remainder = p;(p#YKY)!
if(remainder==0)
return Y!
else
return p
end
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
/in)lude0math.h-
unsigned int >)d,on+e)ursive(unsigned p1unsigned Y1int n)!
int main(void)
{
int a151i>)d!
)lrs)r()!
printf(34nter the two num5ers whose >CO is to 5e found6 3)!
s)anf(3dd317a175)!
printf(3ddd<n31a151>)d,on+e)ursive(a151n))!
get)h()!
$
unsigned int >)d,on+e)ursive(unsigned p1unsigned Y)
{
unsigned remainder!
remainder = p;(p#YKY)!
if(remainder==0)
return Y!
else
return p
$
Input: 4nter the two num5ers whose >CO is to 5e found6 & 10
Output: &
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%&
) AI!6 C programs that use ,on;re)ursive fun)tions to solve (owers of ?anoi
pro5lem.
Algorithm:
+onrecur&i1e:
1. start
2. read num
3. one6
if(num==1)
{
.rite M'ove top dis2 from needle ) to needle ) 31 sndl1dndl!
goto four!
$
two6
top=top"1!
st2nPtopQ=num!
st2sndlPtopQ=sndl!
st2indlPtopQ=indl!
st2dndlPtopQ=dndl!
st2addPtopQ=3!
num=num;1!
sndl=sndl!
temp=indl!
indl=dndl!
dndl=temp!
goto one!
%. three6
printf(3<n'ove top dis2 from needle ) to needle ) 31sndl1dndl)!
top=top"1!
st2nPtopQ=num!
st2sndlPtopQ=sndl!
st2indlPtopQ=indl!
st2dndlPtopQ=dndl!
st2addPtopQ=&!
num=num;1!
temp=sndl!
sndl=indl!
indl=temp!
dndl=dndl!
goto one!
&. four6
if(top==,E==)
return!
num=st2nPtopQ!
sndl=st2sndlPtopQ!

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%8
indl=st2indlPtopQ!
dndl=st2dndlPtopQ!
add=st2addPtopQ!
top=top;1!
if(add==3)
goto three!
else if(add==&)
goto four!
8. end
Program code:
2in)lude0)onio.h-
/in)lude0stdio.h-
void hanoi,on+e)ursion(int num1)har sndl1)har indl1)har dndl)
{
Char st2dndlP100Q1st2addP100Q1temp!
Char st2nP100Q1st2sndlP100Q1st2indlP100Q!
int top1add!
top=,E==!
Qrintf(MdN1 7num)!
one6
if(num==1)
{
printf(3<n'ove top dis2 from needle ) to needle
) 31sndl1dndl)!
goto four!
$
two6
top=top"1!
st2nPtopQ=num!
st2sndlPtopQ=sndl!
st2indlPtopQ=indl!
st2dndlPtopQ=dndl!
st2addPtopQ=3!
num=num;1!
sndl=sndl!
temp=indl!
indl=dndl!
dndl=temp!
goto one!
three6
printf(3<n'ove top dis2 from needle ) to needle ) 31sndl1dndl)!
top=top"1!
st2nPtopQ=num!
st2sndlPtopQ=sndl!

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%G
st2indlPtopQ=indl!
st2dndlPtopQ=dndl!
st2addPtopQ=&!
num=num;1!
temp=sndl!
sndl=indl!
indl=temp!
dndl=dndl!
goto one!
four6
if(top==,E==)
return!
num=st2nPtopQ!
sndl=st2sndlPtopQ!
indl=st2indlPtopQ!
dndl=st2dndlPtopQ!
add=st2addPtopQ!
top=top;1!
if(add==3)
goto three!
else if(add==&)
goto four!
$
Input: 2
Output: 'ove dis2 1 form a to 5
'ove dis2 2 from a to )
'ove dis2 1 from 5 to )
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%C
AI! 6 C programs that use re)ursive fun)tions to solve (owers of ?anoi
pro5lem
Algorithm:
1. start
2. read no
3. if(no01)
.rite (hereWs nothing to move.
else
write +e)ursive)all hanoi+e)ursion(no1WAW1WBW1WCW)!
%. end
1.start
2.if ( num == 1 )
{
.rite 3'ove top dis2 from needle ) to needle ).31
ndl11 ndl2
return!
$
3. .rite 3'ove top dis2 from needle ) to needle ).31 ndl11 ndl2
%. )all hanoi+e)ursion( num ; 11ndl31 ndl21 ndl1 )!
&. end
Program code:
void main()
{
int no!
)lrs)r()!
printf(34nter the no. of dis2s to 5e transferred6 3)!
s)anf(3d317no)!
if(no01)
printf(3<n(hereWs nothing to move.3)!
else
printf(3,on;+e)ursive3)!
hanoi,on+e)ursion(no1WAW1WBW1WCW)!
printf(3<n+e)ursive3)!
hanoi+e)ursion(no1WAW1WBW1WCW)!
get)h()!
$
void hanoi+e)ursion( int num1)har ndl11 )har ndl21 )har ndl3)
{
if ( num == 1 )
{
printf( 3'ove top dis2 from needle ) to needle ).31 ndl11 ndl2 )!

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
%L
return!
$
hanoi+e)ursion( num ; 11ndl11 ndl31 ndl2 )!
printf( 3'ove top dis2 from needle ) to needle ).31 ndl11 ndl2 )!
hanoi+e)ursion( num ; 11ndl31 ndl21 ndl1 )!
$
Input: 2
Output: 'ove dis2 1 form a to 5
'ove dis2 2 from a to )
'ove dis2 1 from 5 to )
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&0
COMPUTER PROGRAMMING LAB
WEEK-1.
AI! 6 C program that uses fun)tions to perform the following operations6
i) +eading a )ompleH num5er
ii) .riting a )ompleH num5er
iii) Addition of two )ompleH num5ers
iv) 'ultipli)ation of two )ompleH num5ers
Algorithm:
1. start
2. read opern
3. if opern==0
4Hit
% if opern==1 XX 2
Call arithmeti)(opern)
&. end
1.start
2 read sr11 str2
3. if op==1
Call add
4lse
Call mul
%.end
1. start
2. stru)t )ompleHadd( stru)t )ompleH H11 stru)t )ompleH H2)
{
stru)t )ompleH H3!
S3.real=H1.real"H2.real!
return (H3)!
$
3. end
1. start
2. stru)t )ompleHmul(stru)t )ompleH H11 stru)t )ompleH H2)
{
H3.realpart=(H1.realpartKH2.realpart);
(H1.imgpartKH2.imgpart)!

H3.imgpart=(H1.realpartKH2.imgpart)"
(H1.imgpartKH2.realpart)

return(H3)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&1
$
3. end
Program code:
/in)lude0stdio.h-
/in)lude0math.h-
void arithmeti)(int opern)!
stru)t )omp
{
dou5le realpart!
dou5le imgpart!
$!
void main()
{
int opern!
)lrs)r()!
printf(3<n<n <t<t<tKKKKK 'AA, '4,E KKKKK3)!
printf(3<n<n *ele)t Dour option6 <n
1 6 AOO<n 2 6 'E=(A9=U<n 0 6 4SA( <n<n<t<t
4nter Dour @ption P Q<5<53)!
s)anf(3d317opern)!
swit)h(opern)
{
)ase 06
eHit(0)!
)ase 16
)ase 26
arithmeti)(opern)!
default6
main()!
$
$
void arithmeti)(int opern)
{
stru)t )omp w11 w21 w!
printf(3<n 4nter two CompleH ,um5ers (H"iD)6<n +eal 9art of :irst
,um5er63)!
s)anf(3lf317w1.realpart)!
printf(3<n AmaginarD 9art of :irst ,um5er63)!
s)anf(3lf317w1.imgpart)!
printf(3<n +eal 9art of *e)ond ,um5er63)!
s)anf(3lf317w2.realpart)!

printf(3<n AmaginarD 9art of *e)ond ,um5er63)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&2
s)anf(3lf317w2.imgpart)!
swit)h(opern)
{
)ase 16
w.realpart = w1.realpart"w2.realpart!
w.imgpart = w1.imgpart"w2.imgpart!
5rea2!
)ase 26
w.realpart=(w1.realpartKw2.realpart);
w1.imgpartKw2.imgpart)!
w.imgpart=(w1.realpartKw2.imgpart)"
(w1.imgpartKw2.realpart)!
5rea2!
$
if (w.imgpart-0)
printf(3<n Answer = lf"lfi31w.realpart1w.imgpart)!
else
printf(3<n Answer = lflfi31w.realpart1w.imgpart)!
get)h()!
main()!
$

Input6 4nter two CompleH ,um5ers (H"iD)6
+eal 9art of :irst ,um5er6%
imaginarD 9art of :irst ,um5er68
+eal 9art of *e)ond ,um5er6&
AmaginarD 9art of *e)ond num5erer68
Output : *ele)t Dour option6
1 6 AOO
2 6 'E=(A9=U
3 6 4SA(
4nter Dour @ption 1
A,*.4+6 L"12i
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&3
COMPUTER PROGRAMMING LAB
WEEK-13
A)AI!: .rite a program to a))ept the elements of the stru)ture emploDee;name1 Basi)
paD and displaD the same stru)ture along with the OA1 CCA and >ross salarD.
OA=&1 of Basi);9aD.
CCA=+s.100.)onsolidated.
Program code:
/in)lude0stdio.h-
/in)lude0)onio.h-
/define CCA 100
main ()
{
stru)t emploDee
{
)har 4mploDTnameP20Q!
float Basi)TpaD!
float OA!
float >rossTsalarD!
$!
stru)t emploDee eP&Q!
int i!
float H1Kp!
)lrs)r()!
p=7H!
H=Kp!
printf(34nter name and 5asi) salarD of empoloDees66<n3)!
for(i=0!i0&!i"")
{
s)anf(3sf317ePiQ.4mploDTname17ePiQ.Basi)TpaD)!
$
printf(34mploDTname Basi)TpaD OA CCA >rossTsalarD<n3)!
for(i=0!i0&!i"")
{
ePiQ.OA=ePiQ.Basi)TpaDK(float)&1#100!
ePiQ.>rossTsalarD=ePiQ.Basi)TpaD"ePiQ.Basi)TpaDK(float)&1#100"CCA!
printf(3s .2f .2f d .2f<n31<
ePiQ.4mploDTname1ePiQ.Basi)TpaD1ePiQ.OA1CCA1ePiQ.>rossTsalarD)!
$
get)h()!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&%
Input & Output:
4nter name and 5asi) salarD of emploDees6
'unwar L000
Uasmin &000
=u)2D G000
Vian 8000
Vvs2 GGCL
Emplo-4name Ba&ic4pa- 5A A 6ro&&4&alar-
'unwar L000.00 %&L0.00 100 138L0.00
Uasmin &000.00 2&&0.00 100 G8&0.00
=u)2D G000.00 3&G0.00 100 108G0.00
Vian 8000.00 3080.00 100 L180.00
Vvs2 GGCL.00 3LG2.3L 100 11C81.3L
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&&
B)AI!: Oefine a stru)ture to store emploDeeZs data with the following spe)ifi)ations6
4mploDee; ,um5er1 4mploDee;,ame1 4mploDeeZs paD1 date of Roining
i) .rite a program to store 10 emploDee details.
ii) .rite a program to implement the following de)ision an revision of salaries6
paD0=+s.&000 in)rease 5D 1&
paD0=+s.2&000 in)rease 5D 10
paD-+s.2&000 no in)rease.
iii) .rite a fun)tion to print details of emploDees who have )ompleted 20 Dears
servi)e on a spe)ified data.
Program code:
/in)lude0dos.h-
/in)lude0stdio.h-
/in)lude0)onio.h-
stru)t do5
{
int daD!
int month!
int Dear!
$!
stru)t emploDee
{
int 4Tnum!
)har 4TnameP20Q!
float BTpaD!
stru)t do5 d1!
$!
void store(int)!
void paDrevising()!
void print()!
stru)t emploDee eP10Q!
int i1n!
void main()
{
int )h=0!
)lrs)r()!
printf(34nter how manD emploDees663)!
s)anf(3d317n)!
do
{
printf(3<n1.data entrD<n2.salarD revising<n3.displaD<n3)!
printf(3sele)t the operation Dou want to perform(L;eHit)66<n3)!
s)anf(3d317)h)!
swit)h()h)
{

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&8
)ase 16
store(n)!
5rea2!
)ase 26
paDrevising()!
5rea2!
)ase 36
print()!
5rea2!
default6
printf(3invalid )hoi)e3)!
5rea2!
$
$while()h!=L)!
get)h()!
$
void store(int n)
{
printf(34nter 4Tnum14Tname1Basi) paD and O@B(dd#mm#DDDD66<n3)!
for(i=1!i0=n!i"")
{
printf(3<nPdQ;31i)!
s)anf(3dsfd#d#d317ePiQ.4Tnum1ePiQ.4Tname17ePiQ.BTpaD1
7ePiQ.d1.daD17ePiQ.d1.month17ePiQ.d1.Dear)!
$
printf(3<n<nstored data su)essfullD3)!
return!
$
void paDrevising()
{
for(i=0!i0=n!i"")
{
if(ePiQ.BTpaD0=&000)
ePiQ.BTpaD"=(ePiQ.BTpaDK1&#(float)100)!
if(ePiQ.BTpaD-&000 77 ePiQ.BTpaD0=2&000)
ePiQ.BTpaD"=(ePiQ.BTpaDK10#(float)100)!
$
return!
$
void print()
{
stru)t date O!
getdate(7O)!
printf(34Tnum 4Tname Basi) paD O@B<n3)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&G
for(i=1!i0=n!i"")
printf(3dsfd#d#d<n31ePiQ.4Tnum1ePiQ.4Tname1ePiQ.BTpaD1
ePiQ.d1.daD1ePiQ.d1.month1ePiQ.d1.Dear)!
printf(3<n4mploDes who are )ompleted 20 Dears are66<n3)!
for(i=1!i0=n!i"")
{
if(O.daTDear;ePiQ.d1.Dear-=0)
printf(3d s f d#d#d<n31ePiQ.4Tnum1ePiQ.4Tname1ePiQ.BTpaD1ePiQ.
d1.daD1ePiQ.d1.month1ePiQ.d1.Dear)!
return!
$
$
Input & Output:
4nter how manD emploDees66
2
1.data entrD
2.salarD revising
3.displaD
sele)t the operation Dou want to perform(L;eHit)6 1
4nter 4Tnum14Tname1Basi) paD and O@B(dd#mm#DDDD66
P1Q;12 2iran &000 1LGL
P2Q;1& poornima 3000 1LL3
stored data su))essfullD
1.data entrD
2.salarD revising
3.displaD
sele)t the operation Dou want to perform(L;eHit)66 3
4Tnum 4Tname Basi)T paD O@B
12 2iran &000.00 1LGL#2#01
1& poornima 3000.00 1LL3#30#3
4mploDes who are )ompleted 20 Dears are66
12 2iran &000.000000 1LGL#2#01
1.data entrD
2.salarD revising
3.displaD
sele)t the operation Dou want to perform(L;eHit)62
1.data entrD
2.salarD revising
3.displaD
sele)t the operation Dou want to perform(L;eHit)663
4Tnum 4Tname Basi) paD O@B
12 2iran 832&.000000 1LGL#0#0
1& poornima 3%&0.000000 1LL3#0#0
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&C
4mploDes who are )ompleted 20 Dears are66
12 2iran 832&.000000 1LGL#0#0
1.data entrD
2.salarD revising
3.displaD
sele)t the operation Dou want to perform(L;eHit)6L
invalid )hoi)e
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
&L
COMPUTER PROGRAMMING LAB
WEEK-17
A) AI!: Write a program which copie& one %ile to another$
Algorithm:
*tep16start
*tep26de)lare two file pointers fs1ft!
*tep36open file1 using fs in read mode!
*tep%6open file2 using ft in read mode!
*tep&6read )hara)ter 5D )hara)ter from file1 and store it in file2!
*tep86 stop
Program code:
/in)lude 0stdio.h-
/in)lude 0)onio.h-
/in)lude 0pro)ess.h-
void main(int arg)1 )har KargvPQ)
{
:A=4 Kfs1Kft!
)har )h!
)lrs)r()!
if(arg)!=3)
{
puts(3Anvalid num5er of arguments.3)!
eHit(0)!
$
fs = fopen(argvP1Q13r3)!
if(fs==,E==)
{
puts(3*our)e file )annot 5e opened.3)!
eHit(0)!
$
ft = fopen(argvP2Q13w3)!
if (ft==,E==)
{
puts(3(arget file )annot 5e opened.3)!
f)lose(fs)!
eHit(0)!
$

while(1)
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
80
{
)h=fget)(fs)!
if ()h==4@:)
5rea2!
else
fput)()h1ft)!
$
f)lose(fs)!
f)lose(ft)!
get)h()!
$
Input & Output:
Contents of file;1 )opied into file;2
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
81
B)AI!: Write a program to re1er&e the %ir&t n character& in a %ile$
(,ote6 (he file name and n are spe)ified on the )ommand line.)
Algorithm:
*tep16start
*tep 26de)lare two file pointers fs1n!
*tep 36read the n value
*tep %6open file1 using fs in read mode!
*tep &6move the file pointer to n position of file1!
*tep 86read )hara)ter 5D )hara)ter in 5a)2word from file 1.
*tep G6 store it in a string!
*tep C6print that string!
*tep L6 stop
Program code:
/in)lude 0stdio.h-
/in)lude 0)onio.h-
/in)lude 0string.h-
/in)lude 0pro)ess.h-
void main(int arg)1 )har KargvPQ)
{
)har aP1&Q!
)har sP20Q!
)har n!
int 2!
int R=0!
int i!
int len!
:A=4 Kfp!
if(arg)!=3)
{
puts(3Amproper num5er of arguments.3)!
eHit(0)!
$
fp = fopen(argvP1Q13r3)!
if(fp == ,E==)
{
puts(3:ile )annot 5e opened.3)!
eHit(0)!
$
2=KargvP2Q;%C!
n = fread(a11121fp)!

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
82
aPnQ=W<0W!
len=strlen(a)!
for(i=len;1!i-=0!i;;)
{
sPRQ=aPiQ!
printf(3)31sPRQ)!
R=R"1!
$
sPR"1Q=W<0W!
get)h()!
$
Input & Output:
aydiv
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
83
COMPUTER PROGRAMMING LAB
WEEK-18
AI!: .rite a program to indi)ate the preparation of a 5illing sDstem at a 5usiness store
5ased on the )ustomer AO as des)ri5ed in the 5elow ta5le.
Input File:
*.,o. Customer AO Atem ,o. \tD. 9ri)e (9er Atem)
1 C*401 A1 2 10
2 C*402 A2 & &0
3 C*401 A2 & &0
% C*403 A% 10 10
Output on &creen:
*.F. 4. C. 9+@FA*A@, *(@+4*
(A+E9A(A
Customer AO6 C*401 Oate6 12;0C;2010
Atem \tD 9ri)e
A1
A2
2
&
20
2&0
(otal 2G0
/in)lude0stdio.h-
/in)lude0dos.h-
/in)lude0)onio.h-
/in)lude0string.h-
void main()
{
int sno1YtD1ppi1i=01flag=0!
)har )idP%Q1inoP3Q1)ustidP%Q!
:A=4 Kfp!
stru)t date d1!
)lrs)r()!
getdate(7d1)!
fp=fopen(3input.tHt313r3)!
printf(34nter the )ustomer AO663)!
s)anf(3s31)ustid)!
while(i0%)
{
fs)anf(fp13dssdd317sno1)id1ino17YtD17ppi)!
if(str)mp()ustid1)id)==0)

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
8%
{
printf(3<n<n<t<t *.F.9+@FA*A@,*<n3)!
printf(3<t<t<t(A+E9A(?A<n<n3)!
printf(3)ustomer AO=s<t<t<t<tOate=d#d#d<n31<
)id1d1.daTdaD1d1.daTmon1d1.daTDear)!
printf(3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<n3)!
printf(3<n<t<tAtem<tYtD<tpri)e<n3)!
printf(3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<n3)!
printf(3<n<t<ts<td<td<n31ino1YtD1YtDKppi)!
printf(3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<n3)!
printf(3<t<t<t (otal=d31YtDKppi)!
flag=1!
5rea2!
$
else
flag=0!
i""!
$
if(flag==0)
printf(3<nsorrD wrong )ustomer AO<n3)!
f)lose(fp)!
get)h()!
$
Input & Output
4nter the )ustomer AO66C03
*.F.9+@FA*A@,*
(A+E9A(?A
Customer AO=C03 Oate=28#12#200L
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Atem YtD pri)e
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
A3 & 2&0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(otal=2&0

G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
8&
COMPUTER PROGRAMMING LAB
WEEK-1)
AI!: write C programs that implement sta)2 using
1) ArraDs
Program code:
0ource code %or &tac9 operation&: u&ing arra-&:
/ in)lude 0stdio.h-
/ in)lude 0)onio.h-
/ in)lude 0stdli5.h-
/ define 'AS 8
int sta)2P'ASQ!
int top = 0!
int menu()
{
int )h!
)lrs)r()!
printf(M<n ] *ta)2 operations using A++AU... M)!
printf(M<n ;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;<nN)!
printf(M<n 1. 9ush M)!
printf(M<n 2. 9op M)!
printf(M<n 3. OisplaDN)!
printf(M<n %. \uit M)!
printf(M<n 4nter Dour )hoi)e6 M)!
s)anf(MdN1 7)h)!
return )h!
$
void displaD()
{
int A!
if(top == 0)
{
printf(M<n<n*ta)2 emptD..N)!
return!
$
else
{
printf(M<n<n4lements in sta)26N)!
for(A = 0! A 0 top! i"")
printf(M<tdN1 sta)2PiQ)!
$
$
void pop()
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
88
{
if(top == 0)
{
printf(M<n<n*ta)2 Enderflow..N)!
return!
$
else
printf(M<n<npopped element is6 d M1 sta)2P;;topQ)!
$
void push()
{
int data!
if(top == 'AS)
{
printf(M<n<n*ta)2 @verflow..N)!
return!
$
else
{
printf(M<n<n4nter data6 M)!
s)anf(MdN1 7data)!
sta)2PtopQ = data!
top = top " 1!
printf(M<n<nOata 9ushed into the sta)2N)!
$
$
void main()
{
int )h!
do
{
)h = menu()!
swit)h()h)
{
)ase 16
push()!
5rea2!
)ase 26
pop()!
5rea2!
)ase 36
displaD()!
5rea2!
)ase %6
eHit(0)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
8G
$
get)h()!
$ while(1)!
$
Input & Output:
*ta)2 operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. 9ush
2. 9op
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 10
Oata pushed into the *ta)2
*ta)2 operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. 9ush
2. 9op
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 20
Oata pushed into the *ta)2
*ta)2 operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. 9ush
2. 9op
3. OisplaD
%. \uit
4nter Dour )hoi)e6 3
4lements in sta)2
10 20
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
8C
0ource code %or &tac9 operation&: u&ing lin9ed li&t:
/ in)lude 0stdio.h-
/ in)lude 0)onio.h-
/ in)lude 0stdli5.h-
stru)t sta)2
{
int data!
stru)t sta)2 KneHt!
$!
void push()!
void pop()!
void displaD()!
tDpedef stru)t sta)2 node!
node Kstart=,E==!
node Ktop = ,E==!
nodeK getnode()
{
stru)t sta)2 Ktemp!
temp=(node K) mallo)( siJeof(node)) !
printf(M<n 4nter data M)!
s)anf(MdN1 7temp ;- data)!
temp ;- neHt = ,E==!
return temp!
$
void push(node Knewnode)
{
node Ktemp!
if( newnode == ,E== )
{
printf(M<n *ta)2 @verflow..N)!
return!
$
if(start == ,E==)
{
start = newnode!
top = newnode!
$
else
{
temp = start!
while( temp ;- neHt != ,E==)
temp = temp ;- neHt!
temp ;- neHt = newnode!
top = newnode!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
8L
printf(M<n<n<t Oata pushed into sta)2N)!
$
void pop()
{
node Ktemp!
if(top == ,E==)
{
printf(M<n<n<t *ta)2 underflowN)!
return!
$
temp = start!
if( start ;- neHt == ,E==)
{
printf(M<n<n<t 9opped element is d M1 top ;- data)!
start = ,E==!
free(top)!
top = ,E==!
$
else
{
while(temp ;- neHt != top)
{
temp = temp ;- neHt!
$
temp ;- neHt = ,E==!
printf(M<n<n<t 9opped element is d M1 top ;- data)!
free(top)!
top = temp!
$
$
void displaD()
{
node Ktemp!
if(top == ,E==)
{
printf(M<n<n<t<t *ta)2 is emptD M)!
$
else
{
temp = start!
printf(M<n<n<n<t<t 4lements in the sta)26 <nN)!
printf(M&d M1 temp ;- data)!
while(temp != top)
{
temp = temp ;- neHt!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G0
printf(M&d M1 temp ;- data)!
$
$
$
)har menu()
{
)har )h!
)lrs)r()!
printf(M<n <t*ta)2 operations using pointers.. M)!
printf(M<n ;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;<nN)!
printf(M<n 1. 9ush M)!
printf(M<n 2. 9op M)!
printf(M<n 3. OisplaDN)!
printf(M<n %. \uit M)!
printf(M<n 4nter Dour )hoi)e6 M)!
)h = get)he()!
return )h!
$
void main()
{
)har )h!
node Knewnode!
do
{
)h = menu()!
swit)h()h)
{
)ase ^1Z 6
newnode = getnode()!
push(newnode)!
5rea2!
)ase ^2Z 6
pop()!
5rea2!
)ase ^3Z 6
displaD()!
5rea2!
)ase ^%Z6
return!
$
get)h()!
$while( )h != ^%Z )!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G1
Input & Output:
*ta)2 operations using 9ointes
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. 9ush
2. 9op
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 10
Oata pushed into the *ta)2
*ta)2 operations using 9ointers
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. 9ush
2. 9op
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 20
Oata pushed into the *ta)2
*ta)2 operations using 9ointers
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. 9ush
2. 9op
3. OisplaD
%. \uit
4nter Dour )hoi)e6 3
4lements in sta)2
10 20
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G2
COMPUTER PROGRAMMING LAB
WEEK-";
AA'6 write C programs that implement Yueue using
1) ArraDs 2)9ointers
Algorithm:
1.start
2.read )h
3. if )h==1
Call insert()
else Af )h==2
Call delete
4lse if )h==3
Call displaD
4lse if )h==%
4Hit
%.end
Program:
1 )0ource code <ueue operation& u&ing arra-:
/ in)lude 0stdio.h-
/ in)lude 0)onio.h-
/ define 'AS 8
int \P'ASQ!
int front1 rear!
void insert\()
{
int data!
if(rear == 'AS)
{
printf(3<n =inear \ueue is full3)!
return!
$
else
{
printf(3<n 4nter data6 3)!
s)anf(3d31 7data)!
\PrearQ = data!
rear""!
printf(3<n Oata Anserted in the \ueue 3)!
$
$
void delete\()
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G3
{
if(rear == front)
{
printf(3<n<n \ueue is 4mptD..3)!
return!
$
else
{
printf(3<n Oeleted element from \ueue is d31 \PfrontQ)!
front""!
$
$
void displaD\()
{
int i!
if(front == rear)
{
printf(3<n<n<t \ueue is 4mptD3)!
return!
$
else
{
printf(3<n 4lements in \ueue are6 3)!
for(i = front! i 0 rear! i"")
{
printf(3d<t31 \PiQ)!
$
$
$
int menu()
{
int )h!
)lrs)r()!
printf(3<n <t\ueue operations using A++AU..3)!
printf(3<n ;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;<n3)!
printf(3<n 1. Ansert 3)!
printf(3<n 2. Oelete 3)!
printf(3<n 3. OisplaD3)!
printf(3<n %. \uit 3)!
printf(3<n 4nter Dour )hoi)e6 3)!
s)anf(3d31 7)h)!
return )h!
$
void main()
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G%
{
int )h!
do
{
)h = menu()!
swit)h()h)
{
)ase 16
insert\()!
5rea2!
)ase 26
delete\()!
5rea2!
)ase 36
displaD\()!
5rea2!
)ase %6
return!
$
get)h()!
$ while(1)!
Input & Output:
\ueue operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 10
Oata Anserted into the \ueue
\ueue operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 20
Oata Anserted into the \ueue
\ueue operations using A++AU
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G&
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 3
4lements in Yueue
10 20
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
G8
"$0ource code %or =ueue operation& u&ing lin9ed li&t:
/ in)lude 0stdio.h-
/ in)lude 0stdli5.h-
/ in)lude 0)onio.h-
stru)t Yueue
{
int data!
stru)t Yueue KneHt!
$!
tDpedef stru)t Yueue node!
node Kfront = ,E==!
node Krear = ,E==!
nodeK getnode()
{
node Ktemp!
temp = (node K) mallo)(siJeof(node)) !
printf(3<n 4nter data 3)!
s)anf(3d31 7temp ;- data)!
temp ;- neHt = ,E==!
return temp!
$
void insert\()
{
node Knewnode!
newnode = getnode()!
if(newnode == ,E==)
{
printf(3<n \ueue :ull3)!
return!
$
if(front == ,E==)
{
front = newnode!
rear = newnode!
$
else
{
rear ;- neHt = newnode!
rear = newnode!
$
printf(3<n<n<t Oata Anserted into the \ueue..3)!
$
void delete\()
{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
GG
node Ktemp!
if(front == ,E==)
{
printf(3<n<n<t 4mptD \ueue..3)!
return!
$
temp = front!
front = front ;- neHt!
printf(3<n<n<t Oeleted element from Yueue is d 31 temp ;- data)!
free(temp)!
$
void displaD\()
{
node Ktemp!
if(front == ,E==)
{
printf(3<n<n<t<t 4mptD \ueue 3)!
$
else
{
temp = front!
printf(3<n<n<n<t<t 4lements in the \ueue are6 3)!
while(temp != ,E== )
{
printf(3&d 31 temp ;- data)!
temp = temp ;- neHt!
$
$
$
)har menu()
{
)har )h!
)lrs)r()!
printf(3<n <t..\ueue operations using pointers.. 3)!
printf(3<n<t ;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;<n3)!
printf(3<n 1. Ansert 3)!
printf(3<n 2. Oelete 3)!
printf(3<n 3. OisplaD3)!
printf(3<n %. \uit 3)!
printf(3<n 4nter Dour )hoi)e6 3)!
)h = get)he()!
return )h!
$
void main()
{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
GC
)har )h!
do
{
)h = menu()!
swit)h()h)
{
)ase W1W 6
insert\()!
5rea2!
)ase W2W 6
delete\()!
5rea2!
)ase W3W 6
displaD\()!
5rea2!
)ase W%W6
return!
$
get)h()!
$ while()h != W%W)!
$
Input & Output:
\ueue operations using 9ointers
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 10
Oata Anserted into the \ueue
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
GL
\ueue operations using 9ointers
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 20
Oata Anserted into the \ueue
\ueue operations using 9ointers
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 3
4lements in sta)2
10 20
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C0
COMPUTER PROGRAMMING LAB
WEEK-"1
AI!:A C program implement )ir)ular Yueue operations using arraDs.
Program code:
/ in)lude 0stdio.h-
/ in)lude 0)onio.h-
/ define 'AS 8
int C\P'ASQ!
int front = 0!
int rear = 0!
int )ount = 0!
void insertC\()
{
int data!
if()ount == 'AS)
{
printf(3<n Cir)ular \ueue is :ull3)!
$
else
{
printf(3<n 4nter data6 3)!
s)anf(3d31 7data)!
C\PrearQ = data!
rear = (rear " 1) 'AS!
)ount ""!
printf(3<n Oata Anserted in the Cir)ular \ueue 3)!
$
$
void deleteC\()
{
if()ount == 0)
{
printf(3<n<nCir)ular \ueue is 4mptD..3)!
$
else
{
printf(3<n Oeleted element from Cir)ular \ueue is d 31 C\PfrontQ)!
front = (front " 1) 'AS!
)ount ;;!
$
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C1
void displaDC\()
{
int i1 R!
if()ount == 0)
{
printf(3<n<n<t Cir)ular \ueue is 4mptD 3)!
$
else
{
printf(3<n 4lements in Cir)ular \ueue are6 3)!
R = )ount!
for(i = front! R != 0! R;;)
{
printf(3d<t31 C\PiQ)!
i = (i " 1) 'AS!
$
$
$
int menu()
{
int )h!
)lrs)r()!
printf(3<n <t Cir)ular \ueue @perations using A++AU..3)!
printf(3<n ;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;<n3)!
printf(3<n 1. Ansert 3)!
printf(3<n 2. Oelete 3)!
printf(3<n 3. OisplaD3)!
printf(3<n %. \uit 3)!
printf(3<n 4nter Uour Choi)e6 3)!
s)anf(3d31 7)h)!
return )h!
$
void main()
{
int )h!
do
{
)h = menu()!
swit)h()h)
{
)ase 16
insertC\()!
5rea2!
)ase 26
deleteC\()!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C2
5rea2!
)ase 36
displaDC\()!
5rea2!
)ase %6
return!
default6
printf(3<n Anvalid Choi)e 3)!
$
get)h()!
$ while(1)!
$
Input & Output:
Cir)ular \ueue operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 10
Oata Anserted into the \ueue
Cir)ular \ueue operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 1
4nter data 6 20
Oata Anserted into the \ueue
Cir)ular \ueue operations using A++AU
;;;;;;;;;;;KKKKKKKKKK;;;;;;;;;;;;;!
1. Ansert
2. Oelete
3. OisplaD
%. \uit
4nter Dour )hoi)e6 3
4lements in sta)2
10 20
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C3
COMPUTER PROGRAMMING LAB
WEEK-""
AI!: .rite a ) program that uses fun)tions to perform the following operations on
singlD lin2edlist6
i) )reation ii) Ansertion iii)Oeletion iv)(raversal
Algorithm:
reating a 0ingl- *in9ed *i&t with >n? num/er o% node&:
(he following steps are to 5e followed to )reate ^nZ num5er of nodes6
>et the new node using getnode().
newnode = getnode()!
Af the list is emptD1 assign new node as start.
start = newnode!
Af the list is not emptD1 follow the steps given 5elow6
(he neHt field of the new node is made to point the first node (i.e. start
node) in the list 5D assigning the address of the first node.
(he start pointer is made to point the new node 5D assigning the address of
the new node.
+epeat the a5ove steps ^nZ times.
In&erting a node at the /eginning:
(he following steps are to 5e followed to insert a new node at the 5eginning of the list6
>et the new node using getnode().
newnode = getnode()!
Af the list is emptD then start = newnode.
Af the list is not emptD1 follow the steps given 5elow6
newnode ;- neHt = start!
start = newnode!
In&erting a node at the end:

(he following steps are followed to insert a new node at the end of the list6
>et the new node using getnode()
newnode = getnode()!
Af the list is emptD then start = newnode.
Af the list is not emptD follow the steps given 5elow6
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C%
temp = start!
while(temp ;- neHt != ,E==)
temp = temp ;- neHt!
temp ;- neHt = newnode!
In&erting a node at intermediate po&ition:
(he following steps are followed1 to insert a new node in an intermediate position in the
list6
>et the new node using getnode().
newnode = getnode()!
4nsure that the spe)ified position is in 5etween first node and last node. Af not1
spe)ified position is invalid. (his is done 5D )ountnode() fun)tion.
*tore the starting address (whi)h is in start pointer) in temp and prev pointers.
(hen traverse the temp pointer upto the spe)ified position followed 5D prev
pointer.
After rea)hing the spe)ified position1 follow the steps given 5elow6
prev ;- neHt = newnode!
newnode ;- neHt = temp!
=et the intermediate position 5e 3.

5eleting a node at the /eginning:


(he following steps are followed1 to delete a node at the 5eginning of the list6
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
start = start ;- neHt!
free(temp)!
5eleting a node at the end:

(he following steps are followed to delete a node at the end of the list6
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
temp = prev = start!
while(temp ;- neHt != ,E==)
{
prev = temp!
temp = temp ;- neHt!
$
prev ;- neHt = ,E==!
free(temp)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C&
5eleting a node at Intermediate po&ition:
(he following steps are followed1 to delete a node from an intermediate position in the
list (=ist must )ontain more than two node).
Af list is emptD then displaD ^4mptD =istZ message
Af the list is not emptD1 follow the steps given 5elow.
if(pos - 1 77 pos 0 node)tr)
{
temp = prev = start!
)tr = 1!
while()tr 0 pos)
{
prev = temp!
temp = temp ;- neHt!
)tr""!
$
prev ;- neHt = temp ;- neHt!
free(temp)!
printf(3<n node deleted..3)!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
C8
Program code:
0ource ode %or the Implementation o% 0ingle *in9ed *i&t:
/ in)lude 0stdio.h-
/ in)lude 0)onio.h-
/ in)lude 0stdli5.h-
stru)t slin2list
{
int data!
stru)t slin2list KneHt!
$!
tDpedef stru)t slin2list node!
node Kstart = ,E==!
int menu()
{
int )h!
)lrs)r()!
printf(3<n 1.Create a list 3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n 2.Ansert a node at 5eginning 3)!
printf(3<n 3.Ansert a node at end3)!
printf(3<n %.Ansert a node at middle3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n &.Oelete a node from 5eginning3)!
printf(3<n 8.Oelete a node from =ast3)!
printf(3<n G.Oelete a node from 'iddle3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n C.(raverse the list (=eft to +ight)3)!
printf(3<n L.(raverse the list (+ight to =eft)3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n 10. Count nodes 3)!
printf(3<n 11. 4Hit 3)!
printf(3<n<n 4nter Dour )hoi)e6 3)!
s)anf(3d317)h)!
return )h!
$
nodeK getnode()
{
node K newnode!
newnode = (node K) mallo)(siJeof(node))!
printf(3<n 4nter data6 3)!
s)anf(3d31 7newnode ;- data)!
newnode ;- neHt = ,E==!
return newnode!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
CG
int )ountnode(node Kptr)
{
int )ount=0!
while(ptr != ,E==)
{
)ount""!
ptr = ptr ;- neHt!
$
return ()ount)!
$
void )reatelist(int n)
{
int i!
node Knewnode!
node Ktemp!
for(i = 0! i 0 n! i"")
{
newnode = getnode()!
if(start == ,E==)
{
start = newnode!
$
else
{
temp = start!
while(temp ;- neHt != ,E==)
temp = temp ;- neHt!
temp ;- neHt = newnode!
$
$
$
void traverse()
{
node Ktemp!
temp = start!
printf(3<n (he )ontents of =ist (=eft to +ight)6 <n3)!
if(start == ,E==)
{
printf(3<n 4mptD =ist3)!
return!
$
else
while(temp != ,E==)
{
printf(3d;;-31 temp ;- data)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
CC
temp = temp ;- neHt!
$
printf(3 S 3)!
$
void revTtraverse(node Kstart)
{
if(start == ,E==)
{
return!
$
else
{
revTtraverse(start ;- neHt)!
printf(3d ;;-31 start ;- data)!
$
$
void insertTatT5eg()
{
node Knewnode!
newnode = getnode()!
if(start == ,E==)
{
start = newnode!
$
else
{
newnode ;- neHt = start!
start = newnode!
$
$
void insertTatTend()
{
node Knewnode1 Ktemp!
newnode = getnode()!
if(start == ,E==)
{
start = newnode!
$
else
{
temp = start!
while(temp ;- neHt != ,E==)
temp = temp ;- neHt!
temp ;- neHt = newnode!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
CL
$
void insertTatTmid()
{
node Knewnode1 Ktemp1 Kprev!
int pos1 node)tr1 )tr = 1!
newnode = getnode()!
printf(3<n 4nter the position6 3)!
s)anf(3d31 7pos)!
node)tr = )ountnode(start)!
if(pos - 1 77 pos 0 node)tr)
{
temp = prev = start!
while()tr 0 pos)
{
prev = temp!
temp = temp ;- neHt!
)tr""!
$
prev ;- neHt = newnode!
newnode ;- neHt = temp!
$
else
{
printf(3position d is not a middle position31 pos)!
$
$
void deleteTatT5eg()
{
node Ktemp!
if(start == ,E==)
{
printf(3<n ,o nodes are eHist..3)!
return !
$
else
{
temp = start!
start = temp ;- neHt!
free(temp)!
printf(3<n ,ode deleted 3)!
$
$
void deleteTatTlast()
{
node Ktemp1 Kprev!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L0
if(start == ,E==)
{
printf(3<n 4mptD =ist..3)!
return !
$
else
{
temp = start!
prev = start!
while(temp ;- neHt != ,E==)
{
prev = temp!
temp = temp ;- neHt!
$
prev ;- neHt = ,E==!
free(temp)!
printf(3<n ,ode deleted 3)!
$
$
void deleteTatTmid()
{
int )tr = 11 pos1 node)tr!
node Ktemp1 Kprev!
if(start == ,E==)
{
printf(3<n 4mptD =ist..3)!
return !
$
else
{
printf(3<n 4nter position of node to delete6 3)!
s)anf(3d31 7pos)!
node)tr = )ountnode(start)!
if(pos - node)tr)
{
printf(3<n(his node doesnot eHist3)!
$
if(pos - 1 77 pos 0 node)tr)
{
temp = prev = start!
while()tr 0 pos)
{
prev = temp!
temp = temp ;- neHt!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L1
)tr ""!
$
prev ;- neHt = temp ;- neHt!
free(temp)!
printf(3<n ,ode deleted..3)!
$
else
{
printf(3<n Anvalid position..3)!
get)h()!
$
$
$
void main(void)
{
int )h1 n!
)lrs)r()!
while(1)
{
)h = menu()!
swit)h()h)
{
)ase 16
if(start == ,E==)
{
printf(3<n ,um5er of nodes Dou want to )reate6 3)!
s)anf(3d31 7n)!
)reatelist(n)!
printf(3<n =ist )reated..3)!
$
else
printf(3<n =ist is alreadD )reated..3)!
5rea2!
)ase 26
insertTatT5eg()!
5rea2!
)ase 36
insertTatTend()!
5rea2!
)ase %6
insertTatTmid()!
5rea2!
)ase &6
deleteTatT5eg()!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L2
5rea2!
)ase 86
deleteTatTlast()!
5rea2!
)ase G6
deleteTatTmid()!
5rea2!
)ase C6
traverse()!
5rea2!
)ase L6
printf(3<n (he )ontents of =ist (+ight to =eft)6 <n3)!
revTtraverse(start)!
printf(3 S 3)!
5rea2!
)ase 106
printf(3<n ,o of nodes 6 d 31 )ountnode(start))!
5rea2!
)ase 11 6
eHit(0)!
$
get)h()!
$
$
Input & Output:
1.Create a list
;;;;;;;;;;;;;;;;;;;;;;;;;;
2.Ansert a node at 5eginning
3.Ansert a node at end
%.Ansert a node at middle
;;;;;;;;;;;;;;;;;;;;;;;;;;
&.Oelete a node from 5eginning
8.Oelete a node from =ast
G.Oelete a node from 'iddle
;;;;;;;;;;;;;;;;;;;;;;;;;;
C.(raverse the list (=eft to +ight)
L.(raverse the list (+ight to =eft)
;;;;;;;;;;;;;;;;;;;;;;;;;;
10. Count nodes
11. 4Hit
4nter Dour )hoi)e6 1
4nter data 10 20 30
4nter Dour )hoi)e6 C
10 20 30
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L3
COMPUTER PROGRAMMING LAB
WEEK-"#
AI! : A Complete *our)e Code for the Amplementation of Oou5le =in2ed =ist6
Algorithm:
reating a 5ou/le *in9ed *i&t with >n? num/er o% node&:
>et the new node using getnode().
newnode =getnode()!
If the list is empty then start = newnode.
If the list is not empty, follow the steps given below:
(he left field of the new node is made to point the previous node.
(he previous nodes right field must 5e assigned with address of the new
node.
+epeat the a5ove steps ^nZ times.
In&erting a node at the /eginning:
>et the new node using getnode().
newnode=getnode()!
If the list is empty then start = newnode.
If the list is not empty, follow the steps given below:
newnode ;- right = start!
start ;- left = newnode!
start = newnode!
In&erting a node at the end:
Get the new node using getnode()
newnode=getnode();
If the list is empty then start = newnode.
If the list is not empty follow the steps given below:
temp = start!
while(temp ;- right != ,E==)
temp = temp ;- right!
temp ;- right = newnode!
newnode ;- left = temp!
In&erting a node at an intermediate po&ition:
>et the new node using getnode().
newnode=getnode()!
4nsure that the spe)ified position is in 5etween first node and last node. Af not1
spe)ified position is invalid. (his is done 5D )ountnode() fun)tion.
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L%
*tore the starting address (whi)h is in start pointer) in temp and prev pointers.
(hen traverse the temp pointer upto the spe)ified position followed 5D prev
pointer.
After rea)hing the spe)ified position1 follow the steps given 5elow6
newnode ;- left = temp!
newnode ;- right = temp ;- right!
temp ;- right ;- left = newnode!
temp ;- right = newnode!
5eleting a node at the /eginning:
(he following steps are followed1 to delete a node at the 5eginning of the list6
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
start = start ;- right!
start ;- left = ,E==!
free(temp)!
5eleting a node at the end:
Af list is emptD then displaD ^4mptD =istZ message
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
while(temp ;- right != ,E==)
{
temp = temp ;- right!
$
temp ;- left ;- right = ,E==!
free(temp)!
5eleting a node at Intermediate po&ition:
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
>et the position of the node to delete.
4nsure that the spe)ified position is in 5etween first node and last node. Af
not1 spe)ified position is invalid.
(hen perform the following steps6
if(pos - 1 77 pos 0 node)tr)
{
temp = start!
i = 1!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L&
while(i 0 pos)
{
temp = temp ;- right!
i""!
$
temp ;- right ;- left = temp ;- left!
temp ;- left ;- right = temp ;- right!
free(temp)!
printf(3<n node deleted..3)!
$
@ra1er&al and di&pla-ing a li&t A*e%t to Bight):
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
while(temp != ,E==)
{
print temp ;- data!
temp = temp ;- right!
$
@ra1er&al and di&pla-ing a li&t ABight to *e%t):
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
while(temp ;- right != ,E==)
temp = temp ;- right!
while(temp != ,E==)
{
print temp ;- data!
temp = temp ;- left!
$
ounting the +um/er o% +ode&:
int co unt no de( no de *st at)
!
if(st at == "#$$)
et un %;
else
et un(& ' co unt no de(st at ( )ight ));
*

Program code:
/in)lude 0stdio.h-
/in)lude 0stdli5.h-
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
L8
/in)lude 0)onio.h-
stru)t dlin2list
{
stru)t dlin2list Kleft!
int data!
stru)t dlin2list Kright!
$!
tDpedef stru)t dlin2list node!
node Kstart = ,E==!
nodeK getnode()
{
node K newnode!
newnode = (node K) mallo)(siJeof(node))!
printf(3<n 4nter data6 3)!
s)anf(3d31 7newnode ;- data)!
newnode ;- left = ,E==!
newnode ;- right = ,E==!
return newnode!
$
int )ountnode(node Kstart)
{
if(start == ,E==)
return 0!
else
return 1 " )ountnode(start ;- right)!
$
int menu()
{
int )h!
)lrs)r()!
printf(3<n 1.Create3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n 2. Ansert a node at 5eginning 3)!
printf(3<n 3. Ansert a node at end3)!
printf(3<n %. Ansert a node at middle3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n &. Oelete a node from 5eginning3)!
printf(3<n 8. Oelete a node from =ast3)!
printf(3<n G. Oelete a node from 'iddle3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n C. (raverse the list from =eft to +ight 3)!
printf(3<n L. (raverse the list from +ight to =eft 3)!
printf(3<n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
LG
printf(3<n 10.Count the ,um5er of nodes in the list3)!
printf(3<n 11.4Hit 3)!
printf(3<n<n 4nter Dour )hoi)e6 3)!
s)anf(3d31 7)h)!
return )h!
$
void )reatelist(int n)
{
int i!
node Knewnode!
node Ktemp!
for(i = 0! i 0 n! i"")
{
newnode = getnode()!
if(start == ,E==)
start = newnode!
else
{
temp = start!
while(temp ;- right)
temp = temp ;- right!
temp ;- right = newnode!
newnode ;- left = temp!
$
$
$
void traverseTleftTtoTright()
{
node Ktemp!
temp = start!
printf(3<n (he )ontents of =ist6 3)!
if(start == ,E== )
printf(3<n 4mptD =ist3)!
else
while(temp != ,E==)
{
printf(3<t d 31 temp ;- data)!
temp = temp ;- right!
$
$
void traverseTrightTtoTleft()
{
node Ktemp!
temp = start!
printf(3<n (he )ontents of =ist6 3)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
LC
if(start == ,E==)
printf(3<n 4mptD =ist3)!
else
while(temp ;- right != ,E==)
temp = temp ;- right!
while(temp != ,E==)
{
printf(3<td31 temp ;- data)!
temp = temp ;- left!
$
$
void dllTinsertT5eg()
{
node Knewnode!
newnode = getnode()!
if(start == ,E==)
start = newnode!
else
{
newnode ;- right = start!
start ;- left = newnode!
start = newnode!
$
$
void dllTinsertTend()
{
node Knewnode1 Ktemp!
newnode = getnode()!
if(start == ,E==)
start = newnode!
else
{
temp = start!
while(temp ;- right != ,E==)
temp = temp ;- right!
temp ;- right = newnode!
newnode ;- left = temp!
$
$
void dllTinsertTmid()
{
node Knewnode1Ktemp!
int pos1 node)tr1 )tr = 1!
newnode = getnode()!
printf(3<n 4nter the position6 3)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
LL
s)anf(3d31 7pos)!
node)tr = )ountnode(start)!
if(pos ; node)tr -= 2)
{
printf(3<n 9osition is out of range..3)!
return!
$
if(pos - 1 77 pos 0 node)tr)
{
temp = start!
while()tr 0 pos ; 1)
{
temp = temp ;- right!
)tr""!
$
newnode ;- left = temp!
newnode ;- right = temp ;- right!
temp ;- right ;- left = newnode!
temp ;- right = newnode!
$
else
printf(3position d of list is not a middle position 31 pos)!
$
void dllTdeleteT5eg()
{
node Ktemp!
if(start == ,E==)
{
printf(3<n 4mptD list3)!
get)h()!
return !
$
else
{
temp = start!
start = start ;- right!
start ;- left = ,E==!
free(temp)!
$
$
void dllTdeleteTlast()
{
node Ktemp!
if(start == ,E==)
{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
100
printf(3<n 4mptD list3)!
get)h()!
return !
$
else
{
temp = start!
while(temp ;- right != ,E==)
temp = temp ;- right!
temp ;- left ;- right = ,E==!
free(temp)!
temp = ,E==!
$
$
void dllTdeleteTmid()
{
int i = 01 pos1 node)tr!
node Ktemp!
if(start == ,E==)
{
printf(3<n 4mptD =ist3)!
get)h()!
return!
$
else
{
printf(3<n 4nter the position of the node to delete6 3)!
s)anf(3d31 7pos)!
node)tr = )ountnode(start)!
if(pos - node)tr)
{
printf(3<nthis node does not eHist3)!
get)h()!
return!
$
if(pos - 1 77 pos 0 node)tr)
{
temp = start!
i = 1!
while(i 0 pos)
{
temp = temp ;- right!
i""!
$
temp ;- right ;- left = temp ;- left!
temp ;- left ;- right = temp ;- right!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
101
free(temp)!
printf(3<n node deleted..3)!
$
else
{
printf(3<n At is not a middle position..3)!
get)h()!
$
$
$
void main(void)
{
int )h1 n!
)lrs)r()!
while(1)
{
)h = menu()!
swit)h( )h)
{
)ase 1 6
printf(3<n 4nter ,um5er of nodes to )reate6 3)!
s)anf(3d31 7n)!
)reatelist(n)!
printf(3<n =ist )reated..3)!
5rea2!
)ase 2 6
dllTinsertT5eg()!
5rea2!
)ase 3 6
dllTinsertTend()!
5rea2!
)ase % 6
dllTinsertTmid()!
5rea2!
)ase & 6
dllTdeleteT5eg()!
5rea2!
)ase 8 6
dllTdeleteTlast()!
5rea2!
)ase G 6
dllTdeleteTmid()!
5rea2!
)ase C 6
traverseTleftTtoTright()!
5rea2!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
102
)ase L 6
traverseTrightTtoTleft()!
5rea2!
)ase 10 6
printf(3<n ,um5er of nodes6 d31 )ountnode(start))!
5rea2!
)ase 116
eHit(0)!
$
get)h()!
$
$
Input & Output:
1.Create a list
;;;;;;;;;;;;;;;;;;;;;;;;;;
2.Ansert a node at 5eginning
3.Ansert a node at end
%.Ansert a node at middle
;;;;;;;;;;;;;;;;;;;;;;;;;;
&.Oelete a node from 5eginning
8.Oelete a node from =ast
G.Oelete a node from 'iddle
;;;;;;;;;;;;;;;;;;;;;;;;;;
C.(raverse the list (=eft to +ight)
L.(raverse the list (+ight to =eft)
;;;;;;;;;;;;;;;;;;;;;;;;;;
10. Count nodes
11. 4Hit
4nter Dour )hoi)e6 1
4nter data 10 20 30
4nter Dour )hoi)e6 C
10 20 30
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
103
COMPUTER PROGRAMMING LAB
WEEK-"(
AI!: 0ource ode %or ircular 0ingle *in9ed *i&t:
Algorithm:
reating a circular &ingle *in9ed *i&t with >n? num/er o% node&:
>et the new node using getnode().
newnode = getnode()!
If the list is empty, assign new node as stat.
stat = newnode;
If the list is not empty, follow the steps given below:
temp = start!
while(temp ;- neHt != ,E==)
temp = temp ;- neHt!
temp ;- neHt = newnode!
+epeat the a5ove steps ^nZ times.
newnode ;- neHt = start!
In&erting a node at the /eginning:
>et the new node using getnode().
newnode = getnode()!
If the list is empty, assign new node as stat.
start = newnode!
newnode ;- neHt = start!
If the list is not empty, follow the steps given below:
last = start!
while(last ;- neHt != start)
last = last ;- neHt!
newnode ;- neHt = start!
start = newnode!
last ;- neHt = start!
In&erting a node at the end:
Get the new node using getnode().
newnode = getnode()!
If the list is empty, assign new node as stat.
start = newnode!
newnode ;- neHt = start!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
10%
If the list is not empty follow the steps given below:
temp = start!
while(temp ;- neHt != start)
temp = temp ;- neHt!
temp ;- neHt = newnode!
newnode ;- neHt = start!
5eleting a node at the /eginning:
Af the list is emptD1 displaD a message ^4mptD =istZ.
Af the list is not emptD1 follow the steps given 5elow6
last = temp = start!
while(last ;- neHt != start)
last = last ;- neHt!
start = start ;- neHt!
last ;- neHt = start!
After deleting the node1 if the list is emptD then start = NULL.
5eleting a node at the end:
Af the list is emptD1 displaD a message ^4mptD =istZ.
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
prev = start!
while(temp ;- neHt != start)
{
prev = temp!
temp = temp ;- neHt!
$
prev ;- neHt = start!
After deleting the node1 if the list is emptD then start = NULL.
@ra1er&ing a circular &ingle lin9ed li&t %rom le%t to right:
Af list is emptD then displaD ^4mptD =istZ message.
Af the list is not emptD1 follow the steps given 5elow6
temp = start!
do
{
printf(3d 31 temp ;- data)!
temp = temp ;- neHt!
$ while(temp != start)!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
10&
Program code:
/ in)lude 0stdio.h-
/ in)lude 0)onio.h-
/ in)lude 0stdli5.h-
stru)t )slin2list
{
int data!
stru)t )slin2list KneHt!
$!
tDpedef stru)t )slin2list node!
node Kstart = ,E==!
int node)tr!
nodeK getnode()
{
node K newnode!
newnode = (node K) mallo)(siJeof(node))!
printf(3<n 4nter data6 3)!
s)anf(3d31 7newnode ;- data)!
newnode ;- neHt = ,E==!
return newnode!
$
int menu()
{
int )h!
)lrs)r()!
printf(3<n 1. Create a list 3)!
printf(3<n<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n 2. Ansert a node at 5eginning 3)!
printf(3<n 3. Ansert a node at end3)!
printf(3<n %. Ansert a node at middle3)!
printf(3<n<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n &. Oelete a node from 5eginning3)!
printf(3<n 8. Oelete a node from =ast3)!
printf(3<n G. Oelete a node from 'iddle3)!
printf(3<n<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n C. OisplaD the list3)!
printf(3<n L. 4Hit3)!
printf(3<n<n;;;;;;;;;;;;;;;;;;;;;;;;;;3)!
printf(3<n 4nter Dour )hoi)e6 3)!
s)anf(3d31 7)h)!
return )h!
$
void )reatelist(int n)
{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
108
int i!
node Knewnode!
node Ktemp!
node)tr = n!
for(i = 0! i 0 n ! i"")
{
newnode = getnode()!
if(start == ,E==)
{
start = newnode!
$
else
{
temp = start!
while(temp ;- neHt != ,E==)
temp = temp ;- neHt!
temp ;- neHt = newnode!
$
$
newnode ;-neHt = start! #K last node is pointing to starting node K#
$
void displaD()
{
node Ktemp!
temp = start!
printf(3<n (he )ontents of =ist (=eft to +ight)6 3)!
if(start == ,E== )
printf(3<n 4mptD =ist3)!
else
do
{
printf(3<t d 31 temp ;- data)!
temp = temp ;- neHt!
$while(temp != start)!
printf(3 S 3)!
$
void )llTinsertT5eg()
{
node Knewnode1 Klast!
newnode = getnode()!
if(start == ,E==)
{
start = newnode!
newnode ;- neHt = start!
$
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
10G
else
{
last = start!
while(last ;- neHt != start)
last = last ;- neHt!
newnode ;- neHt = start!
start = newnode!
last ;- neHt = start!
$
printf(3<n ,ode inserted at 5eginning..3)!
node)tr""!
$
void )llTinsertTend()
{
node Knewnode1 Ktemp!
newnode = getnode()!
if(start == ,E== )
{
start = newnode!
newnode ;- neHt = start!
$
else
{
temp = start!
while(temp ;- neHt != start)
temp = temp ;- neHt!
temp ;- neHt = newnode!
newnode ;- neHt = start!
$
printf(3<n ,ode inserted at end..3)!
node)tr""!
$
void )llTinsertTmid()
{
node Knewnode1 Ktemp1 Kprev!
int i1 pos !
newnode = getnode()!
printf(3<n 4nter the position6 3)!
s)anf(3d31 7pos)!
if(pos - 1 77 pos 0 node)tr)
{
temp = start!
prev = temp!
i = 1!
while(i 0 pos)
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
10C
{
prev = temp!
temp = temp ;- neHt!
i""!
$
prev ;- neHt = newnode!
newnode ;- neHt = temp!
node)tr""!
printf(3<n ,ode inserted at middle..3)!
$
else
{
printf(3position d of list is not a middle position 31 pos)!
$
$
void )llTdeleteT5eg()
{
node Ktemp1 Klast!
if(start == ,E==)
{
printf(3<n ,o nodes eHist..3)!
get)h()!
return !
$
else
{
last = temp = start!
while(last ;- neHt != start)
last = last ;- neHt!
start = start ;- neHt!
last ;- neHt = start!
free(temp)!
node)tr;;!
printf(3<n ,ode deleted..3)!
if(node)tr == 0)
start = ,E==!
$
$
void )llTdeleteTlast()
{
node Ktemp1Kprev!
if(start == ,E==)
{
printf(3<n ,o nodes eHist..3)!
get)h()!
return !
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
10L
$
else
{
temp = start!
prev = start!
while(temp ;- neHt != start)
{
prev = temp!
temp = temp ;- neHt!
$
prev ;- neHt = start!
free(temp)!
node)tr;;!
if(node)tr == 0)
start = ,E==!
printf(3<n ,ode deleted..3)!
$
$
void )llTdeleteTmid()
{
int i = 01 pos!
node Ktemp1 Kprev!
if(start == ,E==)
{
printf(3<n ,o nodes eHist..3)!
get)h()!
return !
$
else
{
printf(3<n .hi)h node to delete6 3)!
s)anf(3d31 7pos)!
if(pos - node)tr)
{
printf(3<n(his node does not eHist3)!
get)h()!
return!
$
if(pos - 1 77 pos 0 node)tr)
{
temp=start!
prev = start!
i = 0!
while(i 0 pos ; 1)
{
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
110
prev = temp!
temp = temp ;- neHt !
i""!
$
prev ;- neHt = temp ;- neHt!
free(temp)!
node)tr;;!
printf(3<n ,ode Oeleted..3)!
$
else
{
printf(3<n At is not a middle position..3)!
get)h()!
$
$
$
void main(void)
{
int result!
int )h1 n!
)lrs)r()!
while(1)
{
)h = menu()!
swit)h()h)
{
)ase 1 6
if(start == ,E==)
{
printf(3<n 4nter ,o. of nodes to )reate6 3)!
s)anf(3d31 7n)!
)reatelist(n)!
printf(3<n=ist )reated..3)!
$
else
printf(3<n =ist is alreadD 4Hist..3)!
5rea2!
)ase 2 6
)llTinsertT5eg()!
5rea2!
)ase 3 6
)llTinsertTend()!
5rea2!
)ase % 6
)llTinsertTmid()!
5rea2!
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
111
)ase & 6
)llTdeleteT5eg()!
5rea2!
)ase 8 6
)llTdeleteTlast()!
5rea2!
)ase G 6
)llTdeleteTmid()!
5rea2!
)ase C 6
displaD()!
5rea2!
)ase L 6
eHit(0)!
$
get)h()!
$
$
Input & Output:
1.Create a list
;;;;;;;;;;;;;;;;;;;;;;;;;;
2.Ansert a node at 5eginning
3.Ansert a node at end
%.Ansert a node at middle
;;;;;;;;;;;;;;;;;;;;;;;;;;
&.Oelete a node from 5eginning
8.Oelete a node from =ast
G.Oelete a node from 'iddle
;;;;;;;;;;;;;;;;;;;;;;;;;;
C.(raverse the list (=eft to +ight)
L.(raverse the list (+ight to =eft)
;;;;;;;;;;;;;;;;;;;;;;;;;;
10. Count nodes
11. 4Hit
4nter Dour )hoi)e6 1
4nter data 10 20 30
4nter Dour )hoi)e6 C
10 20 30
G.N.Vivekananda, '.(e)h. gnvivekananda@blogspot.in +91 9052307088
Assistant Professor
112

S-ar putea să vă placă și