SlideShare a Scribd company logo
1 of 8
SESSION – 20
RECURSIVE FUNCTION
Session Outcome:
1. Understanding Recursive Functions
2. Examples Of Recursive Functions
Recursive Functions
β€’ Recursive function is closely related to
mathematical induction
β€’ Recursive function is a function that calls itself
β€’ Every Recursive function will have exit or stop
condition
– other wise the recursive function will keep on
calling itself
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
Inherently recursive functions
5!
5*4!
4*3!
3*2!
2*1!
1
5!
5*4!
4*3!
3*2!
2*1!
1
Final value=120
1
2!=2*1=2 returned
3!=3*2=6 returned
4!=4*6=24 returned
5!=5*24=120 returned
Finding Factorial Recursively
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
fact(5)
5*fact(4)
4*fact(3)
3*fact(2)
2*fact(1)1
2
6
24
120
Finding Factorial Recursively
π‘“π‘Žπ‘π‘‘ 𝑛 =
1 𝑖𝑓 𝑛 == 0 π‘œπ‘Ÿ 𝑛 == 1
𝑛 βˆ— π‘“π‘Žπ‘π‘‘ 𝑛 βˆ’ 1 𝑖𝑓 𝑛 > 1
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
Corresponding Recursive Function
int fact(int n)
{
if(n ==1 || n ==0)
return 1;
else
return n*fact(n-1);
}
Finding Factorial Recursively
Complete program is:
#include<stdio.h>
int fact(int);
main()
{
int n;
scanf("%d",&n);
printf("%d",fact(n));
}
int fact(int n)
{
if(n ==1 || n ==0)
return 1;
else
return n*fact(n-1);
}
Sum of Natural Numbers Recursively
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
π‘ π‘’π‘š 𝑛 =
1 𝑖𝑓 𝑛 == 1
𝑛 + π‘ π‘’π‘š 𝑛 βˆ’ 1 𝑖𝑓 𝑛 > 1
Corresponding Recursive Function
int sum(int n)
{
if(n ==1)
return 1;
else
return n + sum(n-1);
}
Finding nth term in Fibonacci Series Recursively
𝑓𝑖𝑏 𝑛 =
0 𝑖𝑓 𝑛 == 0
1 𝑖𝑓 𝑛 == 1
𝑓𝑖𝑏 𝑛 βˆ’ 1 + 𝑓𝑖𝑏 𝑛 βˆ’ 2 𝑖𝑓 𝑛 > 1
Corresponding Recursive Function
int fib(int n)
{
if( n ==0)
return 0;
if( n ==1)
return 1;
else
return fib(n-1)*fib(n-2);
}

More Related Content

What's hot

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
Β 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
Β 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
Β 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
Β 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
Β 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
Β 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
Β 
Access modifiers
Access modifiersAccess modifiers
Access modifiersJadavsejal
Β 
arrays of structures
arrays of structuresarrays of structures
arrays of structuresarushi bhatnagar
Β 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
Β 

What's hot (20)

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
Β 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Β 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
Β 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Β 
File in C language
File in C languageFile in C language
File in C language
Β 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
Β 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
Β 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
Β 
Pointers
PointersPointers
Pointers
Β 
Pointers
PointersPointers
Pointers
Β 
Function Pointer
Function PointerFunction Pointer
Function Pointer
Β 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Β 
Functions in c
Functions in cFunctions in c
Functions in c
Β 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
Β 
Function in C
Function in CFunction in C
Function in C
Β 
Pointers in C
Pointers in CPointers in C
Pointers in C
Β 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
Β 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Β 
C string
C stringC string
C string
Β 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
Β 

More from Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
Β 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
Β 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
Β 
Recursion in C
Recursion in CRecursion in C
Recursion in C
Β 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
Β 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
Β 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
Β 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Β 
Functions
FunctionsFunctions
Functions
Β 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
Β 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
Β 
Graphs
GraphsGraphs
Graphs
Β 
B trees
B treesB trees
B trees
Β 
Functions in python3
Functions in python3Functions in python3
Functions in python3
Β 
Dictionary
DictionaryDictionary
Dictionary
Β 
Sets
SetsSets
Sets
Β 
Lists
ListsLists
Lists
Β 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Β 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Β 
C programs
C programsC programs
C programs
Β 

Recently uploaded

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
Β 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
Β 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
Β 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
Β 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
Β 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
Β 
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...Patryk Bandurski
Β 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
Β 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
Β 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
Β 
Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)
Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)
Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)Wonjun Hwang
Β 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
Β 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
Β 
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
Β 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
Β 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
Β 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
Β 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
Β 
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
Β 

Recently uploaded (20)

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
Β 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Β 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
Β 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
Β 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
Β 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
Β 
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Integration and Automation in Practice: CI/CD in MuleΒ Integration and Automat...
Β 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Β 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
Β 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
Β 
Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)
Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)
Bun (KitWorks Team Study λ…Έλ³„λ§ˆλ£¨ λ°œν‘œ 2024.4.22)
Β 
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
Β 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
Β 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
Β 
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
Β 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
Β 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
Β 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
Β 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
Β 
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
Β 

Recursive functions in C

  • 1. SESSION – 20 RECURSIVE FUNCTION Session Outcome: 1. Understanding Recursive Functions 2. Examples Of Recursive Functions
  • 2. Recursive Functions β€’ Recursive function is closely related to mathematical induction β€’ Recursive function is a function that calls itself β€’ Every Recursive function will have exit or stop condition – other wise the recursive function will keep on calling itself Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in
  • 3. Inherently recursive functions 5! 5*4! 4*3! 3*2! 2*1! 1 5! 5*4! 4*3! 3*2! 2*1! 1 Final value=120 1 2!=2*1=2 returned 3!=3*2=6 returned 4!=4*6=24 returned 5!=5*24=120 returned
  • 4. Finding Factorial Recursively Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in fact(5) 5*fact(4) 4*fact(3) 3*fact(2) 2*fact(1)1 2 6 24 120
  • 5. Finding Factorial Recursively π‘“π‘Žπ‘π‘‘ 𝑛 = 1 𝑖𝑓 𝑛 == 0 π‘œπ‘Ÿ 𝑛 == 1 𝑛 βˆ— π‘“π‘Žπ‘π‘‘ 𝑛 βˆ’ 1 𝑖𝑓 𝑛 > 1 Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in Corresponding Recursive Function int fact(int n) { if(n ==1 || n ==0) return 1; else return n*fact(n-1); }
  • 6. Finding Factorial Recursively Complete program is: #include<stdio.h> int fact(int); main() { int n; scanf("%d",&n); printf("%d",fact(n)); } int fact(int n) { if(n ==1 || n ==0) return 1; else return n*fact(n-1); }
  • 7. Sum of Natural Numbers Recursively Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in π‘ π‘’π‘š 𝑛 = 1 𝑖𝑓 𝑛 == 1 𝑛 + π‘ π‘’π‘š 𝑛 βˆ’ 1 𝑖𝑓 𝑛 > 1 Corresponding Recursive Function int sum(int n) { if(n ==1) return 1; else return n + sum(n-1); }
  • 8. Finding nth term in Fibonacci Series Recursively 𝑓𝑖𝑏 𝑛 = 0 𝑖𝑓 𝑛 == 0 1 𝑖𝑓 𝑛 == 1 𝑓𝑖𝑏 𝑛 βˆ’ 1 + 𝑓𝑖𝑏 𝑛 βˆ’ 2 𝑖𝑓 𝑛 > 1 Corresponding Recursive Function int fib(int n) { if( n ==0) return 0; if( n ==1) return 1; else return fib(n-1)*fib(n-2); }