You are on page 1of 40

# RECURSION

## 1. factorial using iteration

#include<stdio.h>
#include<conio.h>
int factorial(int);
int fact=;
!ain()
"
clrscr();
int n;
#rintf(\$Enter the nu!%er to find its factorial\$);
scanf(\$&d\$'(n);
fact=factorial(n);
#rintf(\$)he factorial of *i+en nu!%er &d is &d\$'n'fact);
,
int factorial(int n)
"
int i;
for(i=n;i>-;i..)
fact=fact/i;
return fact;
,
2. factorial using recursion
#include<stdio.h>
#include<conio.h>
int factorial(int);
int fact=;
!ain()
"
clrscr();
int n;
#rintf(\$Enter the nu!%er to find its factorial\$);
scanf(\$&d\$'(n);
fact=factorial(n);
#rintf(\$)he factorial of *i+en nu!%er &d is &d\$'n'fact);
,
int factorial(int n)
"
if(n==-)
return ;
else
return n/factorial(n.);,
3. multiplication of 2 numbers using iterative addition
#include<stdio.h>
#include<conio.h>
int !ul(int'int);
int c=-;
!ain()
"
clrscr();
int a'%;
#rintf(\$Enter a and % +alues to !ulti#l0\$);
scanf(\$&d&d\$'(a'(%);
c=!ul(a'%);
#rintf(\$)he result is &d\$'c);
,
int !ul(int a'int %)
"
int i=;
for(i=;i<=%;i11)
c=c1a;
return c;
,
4. multiplication of 2 numbers using recursion
#include<stdio.h>
#include<conio.h>
int !ul(int'int);
int c=-;
!ain()
"
clrscr();
int a'%;
#rintf(\$Enter a and % +alues to !ulti#l0\$);
scanf(\$&d&d\$'(a'(%);
c=!ul(a'%);
#rintf(\$)he result is &d\$'c);
,
int !ul(int a'int %)
"
if(%==-)
return %;
if(%==)
return a;
else
return !ul(a'%.)1a;,
5. Fibonacci series using iteration
#include<stdio.h>
#include<conio.h>
fi%(int);
int a=-'%='c=-;
!ain()
"
clrscr();
int n;
#rintf(\$Enter the ran*e to find fi%onacci nu!%ers\$);
scanf(\$&d\$'(n);
fi%(n);
,
fi%(int n)
"
int count=2;
#rintf(\$)he fi%onacci nu!%ers\$);
#rintf(\$&d&d\$'a'%);
do
"
c=a1%;
#rintf(\$&d\$'c);
a=%;
%=c;
count=count1;
,3hile(count<n);
,
6. finding Fibonacci number using recursion
#include<stdio.h>
#include<conio.h>
int fi%(int);
int result=-;
!ain()
"
clrscr();
int n;
#rintf(\$Enter the +alue to find fi%onacci nu!%er\$);
scanf(\$&d\$'(n);
result=fi%(n);
#rintf(\$)he result is &d\$'result);
,
int fi%(int n)
"
if(n==-)
return -;
if(n==)
return ;
else
return fi%(n.)1fi%(n.2);
,
7. finding greatest common divisor of 2 numbers using recursion
#include<stdio.h>
#include<conio.h>
int *cd(int'int);44*reatest co!!on di+isor
int result=-;
!ain()
"
clrscr();
int a'%;
#rintf(\$enter a and % to find its *cd\$);
scanf(\$&d&d\$'(a'(%);
result=*cd(a'%);
#rintf(\$)he result is &d\$'result);
,
int *cd(int a'int %)
"
if(%<=a (( (a&%)==-)
return %;
if(a<%)
return *cd(%'a);
else
return *cd(%'a&%);
,
8. find maximum element in array using recursion
#include<stdio.h>
#include<conio.h>
int !a5i!u!(int67'int'int);
int !a5=-;
!ain()
"
clrscr();
int n'i;
int a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
!a5=a6-7;
!a5=!a5i!u!(a'-'n);
#rintf(\$the !a5i!u! +alue is &d\$'!a5);
,
int !a5i!u!(int a67'int i'int n)
"
if(i>=n)
return !a5;
if(!a5<a6i7)
!a5=a6i7;
i=i1;
!a5i!u!(a'i'n);
,
. find minimum element in t!e array using recursion
#include<stdio.h>
#include<conio.h>
int !ini!u!(int67'int'int);
int !in=-;
!ain()
"
clrscr();
int n'i;
int a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
!in=a6-7;
!in=!ini!u!(a'-'n);
#rintf(\$the !ini!u! +alue is &d\$'!in);
,
int !ini!u!(int a67'int i'int n)
"
if(i>=n)
return !in;
if(!in>a6i7)
!in=a6i7;
i=i1;
!ini!u!(a'i'n);
,
1". find sum of array elements using recursion
#include<stdio.h>
#include<conio.h>
int su!ofarra0ele!ents(int67'int'int);
int su!=-;
!ain()
"
clrscr();
int n'i;
int a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
su!=su!ofarra0ele!ents(a'-'n);
#rintf(\$the su! is &d\$'su!);
,
int su!ofarra0ele!ents(int a67'int i'int n)
"
if(i>=n)
return su!;
su!=su!1a6i7;
i=i1;
su!ofarra0ele!ents(a'i'n);
,
11. find sum of n numbers using recursion
#include<stdio.h>
#include<conio.h>
int su!ofn(int);
int su!=-;
!ain()
"
clrscr();
int n'i;
#rintf(\$Enter the ran*e to find su!\$);
scanf(\$&d\$'(n);
su!=su!ofn(n);
#rintf(\$the su! is &d\$'su!);
,
int su!ofn(int n)
"
if(n==-)
return -;
else
return n1su!ofn(n.);
,
12. searc! an element using linear searc! by iteration
#include<stdio.h>
#include<conio.h>
int linsearch(int67'int'int);
int status=-;
!ain()
"
clrscr();
int n'i'ele!ent)o9eSearched;
int a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
#rintf(\$Enter the ele!ent to %e search\$);
scanf(\$&d\$'(ele!ent)o9eSearched);
status=linsearch(a'n'ele!ent)o9eSearched);
if(status==)
#rintf(\$ele!ent is found\$);
else
,
int linsearch(int a67'int n'int ele)
"
int i;
for(i=-;i<n;i11)
"
if(ele==a6i7)
status=;
else
status=-;
,
return status;
,
13. searc! an element using linear searc! by recursion
#include<stdio.h>
#include<conio.h>
int linsearch(int67'int'int'int);
int status=-;
!ain()
"
clrscr();
int n'i'ele!ent)o9eSearched;
int a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
#rintf(\$Enter the ele!ent to %e search\$);
scanf(\$&d\$'(ele!ent)o9eSearched);
status=linsearch(a'-'n'ele!ent)o9eSearched);
if(status==)
#rintf(\$ele!ent is found\$);
else
,
int linsearch(int a67'int i'int n'int ele)
"
if(i>n)
return -;
else if(ele==a6i7)
return ;
i=i1;
linsearch(a'i'n'ele);
,
14. searc! an element using binary searc! by iteration.
#include<stdio.h>
#include<conio.h>
int %insearch(int67'int'int'int);
int status=-'lo3'hi*h'!id;
!ain()
"
clrscr();
int n'i'ele!ent)o9eSearched'a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
#rintf(\$Enter the ele!ent to %e search\$);
scanf(\$&d\$'(ele!ent)o9eSearched);
hi*h=n.;
!id=(lo31hi*h)42;
status=%insearch(a'ele!ent)o9eSearched'-'hi*h);
if(status==)
#rintf(\$ele!ent is found\$);
else
,
int %insearch(int a67'int ele'int lo3'int hi*h)
"
3hile(lo3<=hi*h)
"
if(ele==a6!id7)
"
status=;
%rea:;
,
if(ele<a6!id7)
hi*h=!id.;
if(ele>a6!id7)
lo3=!id1;
!id=(lo31hi*h)42;
,
return status;
,
15. searc! an element using binary searc! by recursion.
#include<stdio.h>
#include<conio.h>
int %insearch(int67'int'int'int);
int status=-'lo3'hi*h'!id;
!ain()
"
clrscr();
int n'i'ele!ent)o9eSearched'a68-7;
#rintf(\$Enter the ran*e of arra0 ele!ents\$);
scanf(\$&d\$'(n);
for(i=-;i<n;i11)
scanf(\$&d\$'(a6i7);
#rintf(\$Enter the ele!ent to %e search\$);
scanf(\$&d\$'(ele!ent)o9eSearched);
hi*h=n.;
!id=(lo31hi*h)42;
status=%insearch(a'ele!ent)o9eSearched'-'hi*h);
if(status==)
#rintf(\$ele!ent is found\$);
else
,
int %insearch(int a67'int ele'int lo3'int hi*h)
"
if(lo3>hi*h)
return -;
if(ele==a6!id7)
return ;
else if(ele<a6!id7)
"
hi*h=!id.;
!id=(lo31hi*h)42;
return %insearch(a'ele'lo3'hi*h);
,
else if(ele>a6!id7)
"
lo3=!id1;
!id=(lo31hi*h)42;
return %insearch(a'ele'lo3'hi*h);
,
,
#\$%&#'()\$ %*+(,
; recursi+e function !a0 not call itself directl0 rather it !a0 call itself indirectl0 %0 usin*
another recursi+e function.
E*<
;()
"
9()
,
9()
"
;()
,
=ere a calls % 3hich in turn call a 3hich !a0 a*ain calls %. )his entire #rocess is called
chainin*.
#\$%&#'()\$ -\$F(,.(/, /F +01\$2#+(% \$34#\$''(/,'
It(RECURSI>E ?E@IN)ION O@ ;ABE9R;IC ECDRESSIONS5 is an e5a!#le of
recursi+e chainin* #rocess.
Consider the follo3in* *rou# of definitions
. ;n e5#ression is a ter! follo3ed %0 #lus si*n follo3ed %0 ter! or a ter! alone.
2. ; ter! is a factor follo3ed %0 E/F follo3ed %0 factor or factor alone.
G. ; factor is either letter or e5#ression enclosed in #arentheses.
=ere the e5#ression is defined in ter!s' ter!s in ter! of factor' factor in ter!s of
e5#ression.
\$xamples for t!e above definition
+626%6-6\$6F are all factors. .!ey can also be terms. .!ey can also be expressions
since expression may be term alone.
7+5 is factor as 8ell as expression enclosed in parent!eses and it can also be a
term.

+92 can be an expression but not a factor or term from definition 1.

.!e examples mentioned are all valid expressions.
%onsider t!e string
+9:2 is not an expression6term or factor.
,o8 a recursive c!ain !as to be designed by ta;ing t!e input string and c!ec;ing
8!et!er t!e given input string is a valid expression or not.
4rogram
<include=stdio.!>
<include=ctype.!>
<include=string.!>
<define max 1""
int expr7c!ar :6int6int :5?
int term7c!ar :6int6int :5?
int factor7c!ar :6int6int :5?
int getsymb7c!ar :6int6int :5?
main75
@
c!ar strAmaxB?
int len6pos?
printf7C\$nter stringC5?
gets7str5?
lenDstrlen7str5?
posD"?
if7expr7str6len6Epos5DD15
printf7CvalidC5?
else
printf7CinvalidC5?
F
GG gets t!e corresponding c!aracter of string
int getsymb7c!ar strAB6int len6int :pos5
@
c!ar c?
if7:pos=len5
cDstrA:posB?
else
cDH H?
7:pos599?
return c?
F
GGexpr calls t!e term
int expr7c!ar strAB6int len6int :pos5
@
GG c!ec;ing 8!et!er it is a term or not
if7term7str6len6pos5DD"5
return "?
GGgets t!e symbol and c!ec; 8!et!er it is eIual to H9H or not. if not decrement
position since it is pointing to last position of expression
if7getsymb7str6len6pos5JDH9H5
@
printf7CKdC6:pos5?
7:pos5LL?
return 1?
F
return term7str6len6pos5?
F
GGterm calls factor
int term7c!ar strAB6int len6int :pos5
@
GG c!ec;ing 8!et!er it is factor or not
if7factor7str6len6pos5DD"5
return "?
GGgets t!e symbol and c!ec; 8!et!er it is eIual to H:H or not. if not decrement
position since it is pointing to last position of expression
if7getsymb7str6len6pos5JDH:H5
@
printf7CKdC6:pos5?
7:pos5LL?
return 1?
F
return factor7str6len6pos5?
F
GG factor calls expression itself. .!is process is called recursive c!aining.
int factor7c!ar strAB6int len6int :pos5
@
c!ar c?
if77cDgetsymb7str6len6pos55JDH7H5
@
printf7CKcC6c5?
return isalp!a7c5?
F
return 7expr7str6len6pos5 EE getsymb7str6len6pos5DDH5H5?
F

GG (,F(3 ./ 4/'.F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r 8-
char infi56!a5Hter!s7'#ostfi56!a5Hter!s7'stac:Harr6!a5Ho#r7;
int to#=.':=-'i=-;
#ush(char);
char #o#();
int #riorit0(char);
dis#la0();
con+ertInfi5)oDostfi5(char 67'char67);
!ain()
"
clrscr();
#rintf(\$Enter the infi5 e5#ression\$);
*ets(infi5);
con+ertInfi5)oDostfi5(infi5'#ostfi5);
dis#la0();
,
con+ertInfi5)oDostfi5(char infi567'char #ostfi567)
"
char o#r;
if(i>strlen(infi5) (( to#I=.)
"
o#r=#o#();
#ostfi56:7=o#r;
:=:1;
con+ertInfi5)oDostfi5(infi5'#ostfi5);
,
if(i>strlen(infi5) (( to#==.)
"
return -;
,
44 if character scanned is di*it or al#ha%et loo# throu*h di*it or al#ha%et and add it to
tar*et strin*
if(isdi*it(infi56i7) JJ isal#ha(infi56i7))
"
#ostfi56:7=infi56i7;
:=:1;
,
44 if character scanned is o#enin* #aranthesis #ush character into stac:
if(infi56i7==K(K)
"
#ush(infi56i7);
,
44 if character scanned is o#erator co!#are the #riorities
if(infi56i7==K1K JJ infi56i7==K.K JJ infi56i7==K/K JJ infi56i7==K4K JJ infi56i7==KLK JJ infi56i7==K&K)
"
if(to#I=.)
"
o#r=#o#();
if(#riorit0(o#r)>=#riorit0(infi56i7))
"
#ostfi56:7=o#r;
:=:1;
con+ertInfi5)oDostfi5(infi5'#ostfi5);
,
#ush(o#r);
#ush(infi56i7);
,
else
#ush(infi56i7);
,
44 if character scanned is closin* #aranthesis loo# throu*h o#erators #resent in stac: and
#o# the o#erators and add it to tar*et strin* until o#enin* #aranthesis is encountered
if(infi56i7==K)K)
"
o#r=#o#();
if(o#rI=K(K)
"
#ostfi56:7=o#r;
:=:1;
con+ertInfi5)oDostfi5(infi5'#ostfi5);
,
,
i=i1;
con+ertInfi5)oDostfi5(infi5'#ostfi5);
,
#ush(char ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
stac:Harr6to#7=ite!;
,
,
char #o#()
"
char ite!;
if(to#==.)
"
return .;
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
int #riorit0(char c)
"
if(c==KLK)
return G;
if(c==K/K JJ c==K4K JJ c==K&K)
return 2;
if(c==K1KJJ c==K.K)
return ;
else
return -;
,
dis#la0()
"
#rintf(\$)he resultant #ostfi5 e5#ression is\$);
#uts(#ostfi5);
,
GG (,F(3 ./ 4#\$F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r 8-
char infi56!a5Hter!s7'#refi56!a5Hter!s7'stac:Harr6!a5Ho#r7;
int to#=.':=-'i=-;
#ush(char);
char #o#();
int #riorit0(char);
dis#la0();
con+ertInfi5)oDrefi5(char 67'char67);
!ain()
"
clrscr();
#rintf(\$Enter the infi5 e5#ression\$);
*ets(infi5);
strre+(infi5);
:=strlen(infi5).;
con+ertInfi5)oDrefi5(infi5'#refi5);
dis#la0();
,
con+ertInfi5)oDrefi5(char infi567'char #refi567)
"
char o#r;
if(i>strlen(infi5) (( to#I=.)
"
o#r=#o#();
#refi56:7=o#r;
:=:.;
con+ertInfi5)oDrefi5(infi5'#refi5);
,
if(i>strlen(infi5) (( to#==.)
"
return -;
,
44 if character scanned is di*it or al#ha%et loo# throu*h di*it or al#ha%et and add it to
tar*et strin*
if(isdi*it(infi56i7) JJ isal#ha(infi56i7))
"
#refi56:7=infi56i7;
:=:.;
,
44 if character scanned is o#enin* #aranthesis #ush character into stac:
if(infi56i7==K)K)
"
#ush(infi56i7);
,
44 if character scanned is o#erator co!#are the #riorities
if(infi56i7==K1K JJ infi56i7==K.K JJ infi56i7==K/K JJ infi56i7==K4K JJ infi56i7==KLK JJ infi56i7==K&K)
"
if(to#I=.)
"
o#r=#o#();
if(#riorit0(o#r)>#riorit0(infi56i7))
"
#refi56:7=o#r;
:=:.;
con+ertInfi5)oDrefi5(infi5'#refi5);
,
#ush(o#r);
#ush(infi56i7);
,
else
#ush(infi56i7);
,
44 if character scanned is closin* #aranthesis loo# throu*h o#erators #resent in stac: and
#o# the o#erators and add it to tar*et strin* until o#enin* #aranthesis is encountered
if(infi56i7==K(K)
"
o#r=#o#();
if(o#rI=K)K)
"
#refi56:7=o#r;
:=:.;
con+ertInfi5)oDrefi5(infi5'#refi5);
,
,
i=i1;
con+ertInfi5)oDrefi5(infi5'#refi5);
,
#ush(char ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
stac:Harr6to#7=ite!;
,
,
char #o#()
"
char ite!;
if(to#==.)
"
return .;
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
int #riorit0(char c)
"
if(c==KLK)
return G;
if(c==K/K JJ c==K4K JJ c==K&K)
return 2;
if(c==K1KJJ c==K.K)
return ;
else
return -;
,
dis#la0()
"
#rintf(\$)he resultant #refi5 e5#ression is\$);
int 5=-;
3hile(#refi5657==KM-K)
5=51;
3hile(#refi5657I=KM-K)
"
#rintf(\$&c\$'#refi5657);
5=51;
,
,

GG 4/'.F(3 ./ (,F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r 8-
44declare infi5'#ostfi5 and t3o di!ensional stac: arra0
char infi56!a5Hter!s7'#ostfi56!a5Hter!s7'stac:Harr6!a5Ho#r76!a5Ho#r7;
44declare o#erator arra0
44str arra0 is used for storin* di*its or characters
44str2 arra0 is a te!#orar0 arra0 for storin* first o#erand #o##ed fro! stac:
44strG arra0 is a te!#orar0 arra0 for storin* second o#erand #o##ed fro! stac:
char o#r6!a5Hter!s7'str6!a5Hter!s7'str26!a5Hter!s7'strG6!a5Hter!s7;
44 st2 is #ointin* to str2 arra0
44 stG is #ointin* to strG arra0
44in is #ointin* to infi5
char /st2'/stG'/in;
int to#=.'i=-;
#ush(char/);
char /#o#();
dis#la0();
setE5#r();
con+ertDostfi5)oInfi5(char 67'char 67);
!ain()
"
clrscr();
#rintf(\$Enter the #ostfi5 e5#ression\$);
*ets(#ostfi5);
setE5#r();
con+ertDostfi5)oInfi5(#ostfi5'infi5);
dis#la0();
,
setE5#r()
"
st2=(str26-7;
stG=(strG6-7;
in=(infi56-7;
,
con+ertDostfi5)oInfi5(char #ostfi567'char infi567)
"
if(i>strlen(#ostfi5))
return -;
44 if character scanned is di*it or al#ha%et #ush di*it or al#ha%et to stac:
if(isdi*it(#ostfi56i7) JJ isal#ha(#ostfi56i7))
"
str6-7=#ostfi56i7;
str67=KM-K;
#ush(str);
,
44 if character scanned is o#erator #o# the t3o to#!ost o#erands fro! stac: and #ut an
o#erator in %et3een o#erands and #ush the resultant into stac:
if(#ostfi56i7==K1K JJ #ostfi56i7==K.K JJ #ostfi56i7==K/K JJ #ostfi56i7==K4K JJ #ostfi56i7==KLK JJ
#ostfi56i7==K&K)
"
if(to#I=.)
"
st2=#o#();44#o##in* st o#erand
stG=#o#();44#o##in* the 2nd o#erand
o#r6-7=#ostfi56i7;
o#r67=KM-K;
strcat(stG'o#r);44concatenatin* 2nd o#erand and o#erator and result is stored in 2nd
o#erand
strcat(stG'st2);44conactenatin* 2nd o#erand result and st o#erand and result is stored
in st o#erand
#ush(stG);44#ushin* the resultant into stac:
,
,
i=i1;
con+ertDostfi5)oInfi5(#ostfi5'infi5);
,
#ush(char /ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
strc#0(stac:Harr6to#7'ite!);44 storin* ite! in stac: arra0
,
,
char /#o#()
"
char /ite!;
if(to#==.)
"
#rintf(\$stac: is e!#t0\$);,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
dis#la0()
"
#rintf(\$)he resultant infi5 e5#ression is\$);
44#o# the stac: to !a:e stac: content e!#t0
44 declare a te!#orar0 #ointer #ointin* to first #osition of stac: arra0
44 loo# throu*h it and store the +alue in KinK #ointer #ointin* to infi5 e5#ression
char /t=stac:Harr6-7;
3hile(/t)
"
/in=/t;
in11;
t11;
,
#uts(infi5);
,
GG 4/'.F(3 ./ 4#\$F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r --
44declare #refi5'#ostfi5 and t3o di!ensional stac: arra0
char #refi56!a5Hter!s7'#ostfi56!a5Hter!s7'stac:Harr6!a5Ho#r76!a5Ho#r7;
44declare o#erator arra0
44str arra0 is used for storin* di*its or characters
44str2 arra0 is a te!#orar0 arra0 for storin* first o#erand #o##ed fro! stac:
44strG arra0 is a te!#orar0 arra0 for storin* second o#erand #o##ed fro! stac:
char o#r6!a5Hter!s7'str6!a5Hter!s7'str26!a5Hter!s7'strG6!a5Hter!s7;
44 st2 is #ointin* to str2 arra0
44 stG is #ointin* to strG arra0
char /st2'/stG'/#re;
int to#=.'i=-;
#ush(char/);
char/ #o#();
dis#la0();
setE5#r();
con+ertDostfi5)oDrefi5(char 67'char 67);
!ain()
"
clrscr();
#rintf(\$Enter the #ostfi5 e5#ression\$);
*ets(#ostfi5);
setE5#r();
con+ertDostfi5)oDrefi5(#ostfi5'#refi5);
dis#la0();
,
setE5#r()
"
st2=(str26-7;
stG=(strG6-7;
#re=(#refi56-7;
,
con+ertDostfi5)oDrefi5(char #ostfi567'char #refi567)
"
if(i>strlen(#ostfi5) (( to#I=.)
"
strc#0(#re'stac:Harr6to#7);
#re11;
to#=to#.;
con+ertDostfi5)oDrefi5(#ostfi5'#refi5);
,
if(i>strlen(#ostfi5) (( to#==.)
return -;
44 if character scanned is di*it or al#ha%et #ush di*it or al#ha%et to stac:
if(isdi*it(#ostfi56i7) JJ isal#ha(#ostfi56i7))
"
str6-7=#ostfi56i7;
str67=KM-K;
#ush(str);
,
44 if character scanned is o#erator #o# the t3o to#!ost o#erands fro! stac: and #ut an
o#erator %efore o#erands and #ush the resultant into stac:
if(#ostfi56i7==K1K JJ #ostfi56i7==K.K JJ #ostfi56i7==K/K JJ #ostfi56i7==K4K JJ #ostfi56i7==KLK JJ
#ostfi56i7==K&K)
"
st2=#o#();44#o##in* st o#erand
stG=#o#();44#o##in* the 2nd o#erand
o#r6-7=#ostfi56i7;
o#r67=KM-K;
strcat(o#r'stG);44concatenatin* 2nd o#erand and o#erator and result is stored in
o#erator arra0
strcat(o#r'st2);44conactenatin* o#erator arra0 result and st o#erand and result is
stored in o#erator arra0
#ush(o#r);44#ushin* the resultant into stac:
,
i=i1;
con+ertDostfi5)oDrefi5(#ostfi5'#refi5);
,
#ush(char /ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
strc#0(stac:Harr6to#7'ite!);44 storin* ite! in stac: arra0
,
,
char /#o#()
"
char /ite!;
if(to#==.)
"
#rintf(\$stac: is e!#t0\$);
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
dis#la0()
"
#rintf(\$)he resultant #refi5 e5#ression is\$);
#uts(#refi5);
,
GG 4#\$F(3 ./ (,F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r --
44declare #refi5'infi5 and t3o di!ensional stac: arra0
char #refi56!a5Hter!s7'infi56!a5Hter!s7'stac:Harr6!a5Ho#r76!a5Ho#r7;
44declare o#erator arra0
44str arra0 is used for storin* di*its or characters
44str2 arra0 is a te!#orar0 arra0 for storin* first o#erand #o##ed fro! stac:
44strG arra0 is a te!#orar0 arra0 for storin* second o#erand #o##ed fro! stac:
char o#r6!a5Hter!s7'str6!a5Hter!s7'str26!a5Hter!s7'strG6!a5Hter!s7;
44 st2 is #ointin* to str2 arra0
44 stG is #ointin* to strG arra0
44 in is #ointin* to #ostfi5 e5#ression
char /st2'/stG'/in;
int to#=.'i=-;
#ush(char/);
char/ #o#();
dis#la0();
setE5#r();
con+ertDrefi5)oInfi5(char 67'char 67);
!ain()
"
clrscr();
#rintf(\$Enter the #refi5 e5#ression\$);
*ets(#refi5);
setE5#r();
con+ertDrefi5)oInfi5(#refi5'infi5);
dis#la0();
,
setE5#r()
"
strre+(#refi5);
st2=(str26-7;
stG=(strG6-7;
in=(infi56-7;
,
con+ertDrefi5)oInfi5(char #refi567'char infi567)
"
if(i>strlen(#refi5) (( to#I=.)
"
strc#0(in'stac:Harr6to#7);
in11;
to#=to#.;
con+ertDrefi5)oInfi5(#refi5'infi5);
,
if(i>strlen(#refi5) (( to#==.)
return -;
44 if character scanned is di*it or al#ha%et #ush di*it or al#ha%et to stac:
if(isdi*it(#refi56i7) JJ isal#ha(#refi56i7))
"
str6-7=#refi56i7;
str67=KM-K;
#ush(str);
,
44 if character scanned is o#erator #o# the t3o to#!ost o#erands fro! stac: and #ut an
o#erator %et3een o#erands and #ush the resultant into stac:
if(#refi56i7==K1K JJ #refi56i7==K.K JJ #refi56i7==K/K JJ #refi56i7==K4K JJ #refi56i7==KLK JJ
#refi56i7==K&K)
"
st2=#o#();44#o##in* st o#erand
stG=#o#();44#o##in* the 2nd o#erand
o#r6-7=#refi56i7;
o#r67=KM-K;
strcat(st2'o#r);44concatenatin* st o#erand and o#erator and result is stored in st2
strcat(st2'stG);44conactenatin* st2 result and 2nd o#erand and result is stored in st2
#ush(st2);44#ushin* the resultant into stac:
,
i=i1;
con+ertDrefi5)oInfi5(#refi5'infi5);
,
#ush(char /ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
strc#0(stac:Harr6to#7'ite!);44 storin* ite! in stac: arra0
,
,
char /#o#()
"
char /ite!;
if(to#==.)
"
#rintf(\$stac: is e!#t0\$);
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
dis#la0()
"
#rintf(\$)he resultant infi5 e5#ression is\$);
#uts(infi5);
,
GG 4#\$F(3 ./ 4/'.F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r --
44declare #refi5'#ostfi5 and t3o di!ensional stac: arra0
char #refi56!a5Hter!s7'#ostfi56!a5Hter!s7'stac:Harr6!a5Ho#r76!a5Ho#r7;
44declare o#erator arra0
44str arra0 is used for storin* di*its or characters
44str2 arra0 is a te!#orar0 arra0 for storin* first o#erand #o##ed fro! stac:
44strG arra0 is a te!#orar0 arra0 for storin* second o#erand #o##ed fro! stac:
char o#r6!a5Hter!s7'str6!a5Hter!s7'str26!a5Hter!s7'strG6!a5Hter!s7;
44 st2 is #ointin* to str2 arra0
44 stG is #ointin* to strG arra0
44 #ost is #ointin* to #ostfi5 e5#ression
char /st2'/stG'/#ost;
int to#=.'i=-;
#ush(char/);
char/ #o#();
dis#la0();
setE5#r();
con+ertDrefi5)oDostfi5(char 67'char 67);
!ain()
"
clrscr();
#rintf(\$Enter the #refi5 e5#ression\$);
*ets(#refi5);
setE5#r();
con+ertDrefi5)oDostfi5(#refi5'#ostfi5);
dis#la0();
,
setE5#r()
"
strre+(#refi5);
st2=(str26-7;
stG=(strG6-7;
#ost=(#ostfi56-7;
,
con+ertDrefi5)oDostfi5(char #refi567'char #ostfi567)
"
if(i>strlen(#refi5) (( to#I=.)
"
strc#0(#ost'stac:Harr6to#7);
#ost11;
to#=to#.;
con+ertDrefi5)oDostfi5(#refi5'#ostfi5);
,
if(i>strlen(#refi5) (( to#==.)
return -;
44 if character scanned is di*it or al#ha%et #ush di*it or al#ha%et to stac:
if(isdi*it(#refi56i7) JJ isal#ha(#refi56i7))
"
str6-7=#refi56i7;
str67=KM-K;
#ush(str);
,
44 if character scanned is o#erator #o# the t3o to#!ost o#erands fro! stac: and #ut an
o#erator after o#erands and #ush the resultant into stac:
if(#refi56i7==K1K JJ #refi56i7==K.K JJ #refi56i7==K/K JJ #refi56i7==K4K JJ #refi56i7==KLK JJ
#refi56i7==K&K)
"
st2=#o#();44#o##in* st o#erand
stG=#o#();44#o##in* the 2nd o#erand
o#r6-7=#refi56i7;
o#r67=KM-K;
strcat(st2'stG);44concatenatin* st o#erand and 2nd o#erand and result is stored in
st2(#ointin* to str2)
strcat(st2'o#r);44conactenatin* st2 result and o#erator and result is stored in st2
#ush(st2);44#ushin* the resultant into stac:
,
i=i1;
con+ertDrefi5)oDostfi5(#refi5'#ostfi5);
,
#ush(char /ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
strc#0(stac:Harr6to#7'ite!);44 storin* ite! in stac: arra0
,
,
char /#o#()
"
char /ite!;
if(to#==.)
"
#rintf(\$stac: is e!#t0\$);
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
dis#la0()
"
#rintf(\$)he resultant #ostfi5 e5#ression is\$);
#uts(#ostfi5);
,
GG \$)+0&+.(/, /F 4/'.F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<!ath.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r 8-
char #ostfi56!a5Hter!s7;
int stac:Harr6!a5Ho#r7;
int to#=.'result'o#r'o#r2'o#rG'i=-;
#ush(int);
int #o#();
dis#la0();
int e+aluateDostfi5(char 67);
!ain()
"
clrscr();
#rintf(\$Enter the #ostfi5 e5#ression to e+aluate\$);
*ets(#ostfi5);
result=e+aluateDostfi5(#ostfi5);
dis#la0();
,
int e+aluateDostfi5(char #ostfi567)
"
if(i>strlen(#ostfi5) (( to#I=.)
"
int o#r=#o#();
result=o#r;
return result;
,
44 if character scanned is di*it or al#ha%et loo# throu*h di*it or al#ha%et and add it to
tar*et strin*
if(isdi*it(#ostfi56i7) JJ isal#ha(#ostfi56i7))
"
int o#r=#ostfi56i7.K-K;
#ush(o#r);
,
else
"
o#r=#o#();
o#r2=#o#();
s3itch(#ostfi56i7)
"
case K1K<o#rG=o#r21o#r;
%rea:;
case K.K<o#rG=o#r2.o#r;
%rea:;
case K/K<o#rG=o#r2/o#r;
%rea:;
case K4K<o#rG=o#r24o#r;
%rea:;
case K&K<o#rG=o#r2&o#r;
%rea:;
case KLK<o#rG=#o3(o#r2'o#r);
%rea:;
,
#ush(o#rG);
,
i=i1;
e+aluateDostfi5(#ostfi5);
,
#ush(int ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
stac:Harr6to#7=ite!;
,
,
int #o#()
"
int ite!;
if(to#==.)
"
return .;
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
dis#la0()
"
#rintf(\$)he resultant #ostfi5 e5#ression is\$);
#rintf(\$&d\$'result);
,
GG \$)+0&+.(/, /F 4#\$F(3 &'(,1 #\$%&#'(/,
#include<stdio.h>
#include<conio.h>
#include<ct0#e.h>
#include<!ath.h>
#include<strin*.h>
#define !a5Hter!s --
#define !a5Ho#r 8-
char #refi56!a5Hter!s7;
int stac:Harr6!a5Ho#r7;
int to#=.'result'o#r'o#r2'o#rG'i=-;
#ush(int);
int #o#();
dis#la0();
int e+aluateDrefi5(char 67);
!ain()
"
clrscr();
#rintf(\$Enter the #refi5 e5#ression to e+aluate\$);
*ets(#refi5);
strre+(#refi5);
result=e+aluateDrefi5(#refi5);
dis#la0();
,
int e+aluateDrefi5(char #refi567)
"
if(i>strlen(#refi5) (( to#I=.)
"
int o#r=#o#();
result=o#r;
return result;
,
44 if character scanned is di*it or al#ha%et loo# throu*h di*it or al#ha%et and add it to
tar*et strin*
if(isdi*it(#refi56i7) JJ isal#ha(#refi56i7))
"
int o#r=#refi56i7.K-K;
#ush(o#r);
,
else
"
o#r=#o#();
o#r2=#o#();

s3itch(#refi56i7)
"
case K1K<o#rG=o#r1o#r2;
%rea:;
case K.K<o#rG=o#r.o#r2;
%rea:;
case K/K<o#rG=o#r/o#r2;
%rea:;
case K4K<o#rG=o#r4o#r2;
%rea:;
case K&K<o#rG=o#r&o#r2;
%rea:;
case KLK<o#rG=#o3(o#r'o#r2);
%rea:;
,
#ush(o#rG);
,
i=i1;
e+aluateDrefi5(#refi5);
,
#ush(int ite!)
"
if(to#==!a5Ho#r.)
#rintf(\$stac: is full\$);
else
"
to#=to#1;
stac:Harr6to#7=ite!;
,
,
int #o#()
"
int ite!;
if(to#==.)
"
return .;
,
else
"
ite!=stac:Harr6to#7;
to#=to#.;
return ite!;
,
,
dis#la0()
"
#rintf(\$)he resultant #refi5 e5#ression is\$);
#rintf(\$&d\$'result);
,
GG ./M\$#' /F *+,/( &'(,1 #\$%&#'(/,
<include=stdio.!>
<include=conio.!>
to8ers7int6c!ar6c!ar6c!ar5?
GGno of discs
int n?
main75
@
printf7C\$nter t!e number of discsC5?
scanf7CKdC6En5?
GG a is source rod6c is destination rod6b is intermediate rod
to8ers7n6HaH6HcH6HbH5?
F
to8ers7int n6c!ar from6c!ar to6c!ar intermediate5
@
GGonly one disc
if7nDD15
@
printf7CNnKsKcKsKcC6Cmove dis;1 fromC6from6CtoC6to5?
return "?
F
GGmove top nL1 discs from a to b using c as intermediate
to8ers7nL16from6intermediate6to5?
GG move remaining disc from a to c
printf7CNnKsKdKsKcKsKcC6Cmove dis;C6n6CfromC6from6CtoC6to5?
GG move nL1 discs from b to c using a as an intermediate
to8ers7nL16intermediate6to6from5?
F
.o8ers /F !anoi program using lin;ed stac;s
#include <stdio.h>
#include<conio.h>
44 a dis: 3ith a +alue ' 3hich is an ele!ent of the stac: 'to3er in this case
struct ?is:
"
int +alue;
?is:/ ne5t;
,;
struct )o3er 44a stac: data structure re#resentin* a to3er
"
int siNe;
?is:/ current;
,;
+oid #ush(struct )o3er/'int);
+oid #o#(struct )o3er/);
%ool isE!#t0(struct )o3er /);
int *et)o3erSiNe();
+oid #rint)o3er?is:s();
+oid !ain()
"
struct )o3er /source'/destination'/au5illar0;
44)o3ers reOuired for the G to3ers source destination and au5illar0
source=(struct )o3er /)!alloc(siNeof(struct )o3er));
destination=(struct )o3er /)!alloc(siNeof(struct )o3er));
au5illar0=(struct )o3er /)!alloc(siNeof(struct )o3er));
44ta:e nu!%er of dis:s fro! user
int nu!%erOf?is:s;
#rintf(\$Enter nu!%er of ?is:s in the source )o3er\$);
scanf(P&dQ'( nu!%erOf?is:s);
44insertin* the dis:s into the source to3er
createSource)o3er(source'nu!%erOf?is:s);
#rintf(PInitial Scenario of the )o3ers P);
#rint)o3er?is:s (source);
#rintf(\$;u5illar0\$);
#rint)o3er?is:s (au5illar0);
#rintf(\$?estination\$);
#rint)o3er?is:s (destination);
hanoi( nu!%erOf?is:s'source' destination' au5illar0 );
#rintf(\$@inal Scenario of the )o3ers \$);
#rintf(\$Source\$)l;
#rint)o3er?is:s(source);
#rintf(\$;u5illar0Q);
#rint)o3er?is:s (au5illar0);
#rintf(\$?estination\$);
#rint)o3er?is:s (destination);,
>oid #ush(struct )o3er /current)o3er'int ele)
"
Struct ?is: /te!#;
te!#=(struct ?is: /)!alloc(siNeof(struct ?is:));
if(current)o3er.>current==NUAA)
"
te!#.>ne5t=NUAA;
,
else
"
te!#.>ne5t= current)o3er .>current;
,
te!#.>+alue=ele;
current)o3er .>current=te!#;
current)o3er.>siNe11;

,
>oid #o#(struct )o3er /source)
"
if(isE!#t0(source))
"
cout<<\$Mn)o3er is E!#t0Mn\$;
return false;
,
else
"
source.>current=source.>current.>ne5t;
source.>siNe=source.>siNe..;
,
,
Int isE!#t0(struct )o3er /current)o3er)
"
if(*et)o3erSiNe(current)o3er)==-)
return true;
return false;
,
int *et)o3erSiNe(struct )o3er /current)o3er)
"
return current)o3er.>siNe;
,44returns siNe of the )o3er
+oid #rint)o3er?is:s()o3er /curren)o3er)
"
if(isE!#t0(current)o3er))
"
return;
,
Struct ?is: /curr2;
curr2=current)o3er.>current ;

#rintf(\$)o3erMn\$);
int i=-;
3hile(curr2 I=NUAA)
"
if(i>R)
%rea:;
i11;
#rintf(\$&dQ'curr2.>+alue);
curr2=curr2.>ne5t;
,
,44 #rint the )o3er
+oid createSource)o3er()o3er /source'int nu!%erOf?is:s)
"
for(int i=nu!%erOf?is:s;i>-;i..)
"
#ush(source'i);
,
,
+oid !o+e?is:(struct )o3er /source'struct )o3er /dest) 44 !o+inn* a dis: fro! source
to destionation
"
#ush(dest'source.>current.>+alue );
#o#(source);
,
+oid hanoi( int N' struct )o3er /source'struct )o3er /dest'struct )o3er /au5 ) 44 !o+e
N dis:s fro! source to destination
"
if (N > - )
"
hanoi(N . ' source' au5' dest); 44!o+e n. dis:s fro! source to au55ilar0 (su%
#ro%le!)
!o+e?is:(source'dest); 44!o+e n)= dis: fro! source to destination
hanoi(N . ' au5' dest' source); 44!o+e n. dis:s fro! au5illar0 to destination
(su% #ro%le!) ,,
#\$.&#, F#/O F&,%.(/,
M!en t!e function returns6 t!ree actions are performed. .!ey are
1. #eturn address is retrieved and stored in safe location.
2. .!e functions data area is freed. .!is data area contains local variables6
3. Finally branc! is ta;en to return address 8!ic! !ad been previously saved.
.!is restores t!e control to t!e calling routine at t!e point immediately
follo8ing t!e instruction t!at initiated t!e call.
#\$%&#'()\$ '(O&0+.\$- )\$#'(/, /F F+%./#(+0
'truct stac;
@
int top?
int paramAO+3'.+%PB?
F?
int simfact7int n5
@
struct stac; s?
s!ort int und?
int x?
int y?
s.topDL1?
xDn?
startQ if7xDD"5
yD1?
else
@
4us!7Es6xLL5?
goto start?
F
0abel1Q popandtest7Es6Ex6Eund5?
(f7undDD.#&\$5
#eturn y?
y:Dx?
goto 0abel1?
F
,/, #\$%&#'()\$ '(O&0+.\$- )\$#'(/, /F ./M\$#' /F *+,/(
Struct dataarea
"
int #ara!;
char fro!;
char to;
char au5;
,
Struct stac:
"
int to#;
struct dataarea ite!6S;CS);CT7;
,
>oid si!to3ers(int n'char fro!'char to 'char au5)
"
Struct dataarea cur#ar;
char te!#;
cur#ar.#ara!=n;
cur#ar.fro!=fro!;
cur#ar.to=to;
cur#ar.au5=au5;
start< if(cur#ar.#ara!==)44 no of disc =
"
Drintf(P!o+e dis: fro! P1fro!1QtoQ1to);
#o#((s'(cur#ar);
s3itch(i)
"
Case < *oto la%el;
Case 2<*oto Aa%el2;
Case G< *oto Aa%el G;
,
,
Dush((s'(cur#ar);
cur#ar.#ara!=cur#ar.#ara!.;
te!#=cur#ar.au5;
cur#ar.au5=cur#ar.to;
cur#ar.to=te!#;
*oto start;
Aa%el 2< Drintf(P!o+e dis: fro! P1fro!1QtoQ1to);
Dush((s'(cur#ar);
cur#ar.#ara!=cur#ar.#ara!.;
te!#=cur#ar.fro!;
cur#ar.fro!=cur#ar.au5;
cur#ar.au5=te!#;
*oto start;
#o#((s'(cur#ar);
s3itch(i)
"
Case < *oto la%el;
Case 2<*oto Aa%el2;
Case G< *oto Aa%el G;
,
Aa%el< return;
,

\$FF(%(\$,%R /F #\$%&#'(/,
. )he !aUor o+erhead of recursion is the !e!or0 it occu#ies and the e5ecution
ti!e.
2. ; non recursi+e #ro*ra! reOuires !ini!u! !e!or0 and less ti!e for e5ecution
as co!#ared to recursi+e function.
G. )he recursi+e function uses stac: to #ush and #o# the local +aria%les.
R. In a non recursi+e function the a%o+e #ush and #o# o#erations 3ith stac: are
s:i##ed. If a #art of #ro*ra! is to %e in+o:ed freOuentl0 then non recursi+e
solution is the %est !ethod to %e ado#ted.