SlideShare une entreprise Scribd logo
1  sur  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);
}

Contenu connexe

Tendances

Tendances (20)

Strings in C
Strings in CStrings in C
Strings in C
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Enums in c
Enums in cEnums in c
Enums in c
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Variadic functions
Variadic functionsVariadic functions
Variadic functions
 
C Language
C LanguageC Language
C Language
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Type conversion
Type conversionType conversion
Type conversion
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Slicing
SlicingSlicing
Slicing
 
C Tokens
C TokensC Tokens
C Tokens
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
 
Comments in C Programming
Comments in C ProgrammingComments in C Programming
Comments in C Programming
 
Structures
StructuresStructures
Structures
 

Plus de Lakshmi Sarvani Videla

Plus de 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
 

Dernier

Dernier (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

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); }