SlideShare a Scribd company logo
1 of 18
Recursion
• 5! = 5 * 4 * 3 * 2 * 1
• 5! = 5 * 4!
• 4! = 4 * 3!...
n ! =
1
n*(n-1)! (n>1)
s=1;
n=1;
while (n<=20)
s = n * s;
n! = n * (n-1)!
Recursion
#include <stdio.h>
long fac(int n) {
long f;
if (n==0||n==1)
f=1;
else
f=n*fac(n-1);
printf("t%d!=%ldn", n, f);
return f;
}
void main( ) {
printf("nt5!=%ldn", fac(5));
}
1!=1
2!=2
3!=6
4!=24
5!=120
5!=120
Recursion
5!
5*4!
4*3!
3*2!
2*1!
1
120
5*24
4*6
3*2
2*1
1
Recursion
0 1 1 2 3 5 8 …
• fib(n) = fib(n-1) + fib(n-2)
• fib(1) = 0
• fib(2) = 1
Storage Classes
int a;
void main() {
int b;
…
}
int max(int x, int y) {
…
}
Automatic Storage
• Auto
• register
auto int a, b;
register int count = 1;
Static Storage
• static
• extern
static int a, b;
extern int total;
Automatic Variables and Static Variables
void func(int a) {
auto int b=10;
static int c=10;
b++; c++;
printf("a=%dtb=%dtc=%dn", a, b, c);
}
void main() {
int i;
for (i=1;i<=3;i++)
func(i);
}
a=1 b=11 c=11
a=2 b=11 c=12
a=3 b=11 c=13
Scope
function scope
file scope
block scope
function prototype scope
File Scope
int total;
int max(int, int);
void main() {
…
}
int limit;
int max(int x, int y) {
…
}
Function Scope
void main() {
…
loop:
…
goto loop;
}
Block Scope
int max(int x, int y) {
…
}
void main() {
int a;
…
{ int a;
…
}
…
}
Function Prototype Scope
int max(int x, int y);
void main() {
…
}
int max(int x, int y) {
…
}
Storage Classes And Scope
#include <stdio.h>
void a(void); /*function prototype*/
void b(void); /*function prototype*/
void c(void); /*function prototype*/
int x = 1; /*global variable*/
void main() {
int x = 5;
printf("local x in outer scope of main is %dn", x);
{ /*start new scope*/
int x = 7;
printf("local x in inner scope of main is %dn", x);
} /*end new scope*/
printf("nlocal x in outer scope of main is %dn", x);
Storage Classes And Scope
a();
b();
c();
a();
b();
c();
printf("local x in main is %dn", x);
}
void a() {
int x = 25; /*initialized each time a is called*/
printf("nlocal x in a is %d after enteringn", x);
x++;
printf("local x in a is %d before exitingn", x);
}
Storage Classes And Scope
void b() {
static int x = 50; /*static initialization only*/
/*first time b is called*/
printf("nlocal x in b is %d after enteringn", x);
x++;
printf("local x in b is %d before exitingn", x);
}
void c() {
printf("nglobal x is %d on entering cn", x);
x*=10;
printf("global x is %d on exiting cn", x);
}
Storage Classes And Scope
local x in outer scope of main is 5
local x in inner scope of main is 7
local x in outer scope of main is 5
local x in a is 25 after entering a
local x in a is 26 before exiting a
local x in b is 50 after entering b
local x in b is 51 before exiting b
global x is 1 on entering c
global x is 10 on exiting c
Storage Classes And Scope
local x in a is 25 after entering a
local x in a is 26 before exiting a
local x in b is 51 after entering b
local x in b is 52 before exiting b
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5

More Related Content

What's hot (20)

Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
week-15x
week-15xweek-15x
week-15x
 
Avl tree
Avl treeAvl tree
Avl tree
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
Tu1
Tu1Tu1
Tu1
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
week-2x
week-2xweek-2x
week-2x
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
week-10x
week-10xweek-10x
week-10x
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
week-1x
week-1xweek-1x
week-1x
 
week-11x
week-11xweek-11x
week-11x
 
Cpl
CplCpl
Cpl
 
C Prog. - Structures
C Prog. - StructuresC Prog. - Structures
C Prog. - Structures
 
Travel management
Travel managementTravel management
Travel management
 

Similar to Session07 recursion

Similar to Session07 recursion (20)

Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Cquestions
Cquestions Cquestions
Cquestions
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
C programming BY Mazedur
C programming BY MazedurC programming BY Mazedur
C programming BY Mazedur
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Vcs16
Vcs16Vcs16
Vcs16
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Numerical Methods in C
Numerical Methods in CNumerical Methods in C
Numerical Methods in C
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
C questions
C questionsC questions
C questions
 
7 functions
7  functions7  functions
7 functions
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
 
Lab Question
Lab QuestionLab Question
Lab Question
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For Google
 

More from HarithaRanasinghe (20)

Session12 pointers
Session12 pointersSession12 pointers
Session12 pointers
 
Session11 single dimarrays
Session11 single dimarraysSession11 single dimarrays
Session11 single dimarrays
 
Session09 multi dimarrays
Session09 multi dimarraysSession09 multi dimarrays
Session09 multi dimarrays
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Session05 iteration structure
Session05 iteration structureSession05 iteration structure
Session05 iteration structure
 
Session04 selection structure_b
Session04 selection structure_bSession04 selection structure_b
Session04 selection structure_b
 
Session04 selection structure_a
Session04 selection structure_aSession04 selection structure_a
Session04 selection structure_a
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
Session02 c intro
Session02 c introSession02 c intro
Session02 c intro
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
 
Program flow charts
Program flow chartsProgram flow charts
Program flow charts
 
Sad -sample_paper
Sad  -sample_paperSad  -sample_paper
Sad -sample_paper
 
Sad sample paper - mcq answers
Sad   sample paper - mcq answersSad   sample paper - mcq answers
Sad sample paper - mcq answers
 
Paper
PaperPaper
Paper
 
Model questions
Model questionsModel questions
Model questions
 
Model paper algorithms and data structures
Model paper  algorithms and data structuresModel paper  algorithms and data structures
Model paper algorithms and data structures
 
Doc 20180208-wa0001
Doc 20180208-wa0001Doc 20180208-wa0001
Doc 20180208-wa0001
 
Doc 20180130-wa0006
Doc 20180130-wa0006Doc 20180130-wa0006
Doc 20180130-wa0006
 
Doc 20180130-wa0005
Doc 20180130-wa0005Doc 20180130-wa0005
Doc 20180130-wa0005
 
Doc 20180130-wa0004-1
Doc 20180130-wa0004-1Doc 20180130-wa0004-1
Doc 20180130-wa0004-1
 

Recently uploaded

APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 

Recently uploaded (20)

APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 

Session07 recursion

  • 1. Recursion • 5! = 5 * 4 * 3 * 2 * 1 • 5! = 5 * 4! • 4! = 4 * 3!... n ! = 1 n*(n-1)! (n>1) s=1; n=1; while (n<=20) s = n * s; n! = n * (n-1)!
  • 2. Recursion #include <stdio.h> long fac(int n) { long f; if (n==0||n==1) f=1; else f=n*fac(n-1); printf("t%d!=%ldn", n, f); return f; } void main( ) { printf("nt5!=%ldn", fac(5)); } 1!=1 2!=2 3!=6 4!=24 5!=120 5!=120
  • 4. Recursion 0 1 1 2 3 5 8 … • fib(n) = fib(n-1) + fib(n-2) • fib(1) = 0 • fib(2) = 1
  • 5. Storage Classes int a; void main() { int b; … } int max(int x, int y) { … }
  • 6. Automatic Storage • Auto • register auto int a, b; register int count = 1;
  • 7. Static Storage • static • extern static int a, b; extern int total;
  • 8. Automatic Variables and Static Variables void func(int a) { auto int b=10; static int c=10; b++; c++; printf("a=%dtb=%dtc=%dn", a, b, c); } void main() { int i; for (i=1;i<=3;i++) func(i); } a=1 b=11 c=11 a=2 b=11 c=12 a=3 b=11 c=13
  • 9. Scope function scope file scope block scope function prototype scope
  • 10. File Scope int total; int max(int, int); void main() { … } int limit; int max(int x, int y) { … }
  • 11. Function Scope void main() { … loop: … goto loop; }
  • 12. Block Scope int max(int x, int y) { … } void main() { int a; … { int a; … } … }
  • 13. Function Prototype Scope int max(int x, int y); void main() { … } int max(int x, int y) { … }
  • 14. Storage Classes And Scope #include <stdio.h> void a(void); /*function prototype*/ void b(void); /*function prototype*/ void c(void); /*function prototype*/ int x = 1; /*global variable*/ void main() { int x = 5; printf("local x in outer scope of main is %dn", x); { /*start new scope*/ int x = 7; printf("local x in inner scope of main is %dn", x); } /*end new scope*/ printf("nlocal x in outer scope of main is %dn", x);
  • 15. Storage Classes And Scope a(); b(); c(); a(); b(); c(); printf("local x in main is %dn", x); } void a() { int x = 25; /*initialized each time a is called*/ printf("nlocal x in a is %d after enteringn", x); x++; printf("local x in a is %d before exitingn", x); }
  • 16. Storage Classes And Scope void b() { static int x = 50; /*static initialization only*/ /*first time b is called*/ printf("nlocal x in b is %d after enteringn", x); x++; printf("local x in b is %d before exitingn", x); } void c() { printf("nglobal x is %d on entering cn", x); x*=10; printf("global x is %d on exiting cn", x); }
  • 17. Storage Classes And Scope local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local x in b is 50 after entering b local x in b is 51 before exiting b global x is 1 on entering c global x is 10 on exiting c
  • 18. Storage Classes And Scope local x in a is 25 after entering a local x in a is 26 before exiting a local x in b is 51 after entering b local x in b is 52 before exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5