SlideShare a Scribd company logo
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 (20)

Inline function
Inline functionInline function
Inline function
Tech_MX
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
Praveen M Jigajinni
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Templates
TemplatesTemplates
Templates
Pranali Chaudhari
 
Single linked list
Single linked listSingle linked list
Single linked list
jasbirsingh chauhan
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
javaTpoint s
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
karthikeyanC40
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
Digvijay Singh Karakoti
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 

Similar to Recursive functions in C (20)

Recursion
RecursionRecursion
Recursion
Esther Leytush
 
Recursion cps.pptx
Recursion cps.pptxRecursion cps.pptx
Recursion cps.pptx
SivaramakrishnaKapil
 
Recursion c programming.pptx
Recursion c programming.pptxRecursion c programming.pptx
Recursion c programming.pptx
SivaramakrishnaKapil
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
Lec-32 Recursion -Recursion in Computer Science
Lec-32 Recursion -Recursion in Computer ScienceLec-32 Recursion -Recursion in Computer Science
Lec-32 Recursion -Recursion in Computer Science
Anil Yadav
 
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program CorrectnessCMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
allyn joy calcaben
 
Lecture09 recursion
Lecture09 recursionLecture09 recursion
Lecture09 recursion
Hariz Mustafa
 
Lec-6 Recursion of Data Structures & Algorithms
Lec-6 Recursion of Data Structures & AlgorithmsLec-6 Recursion of Data Structures & Algorithms
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
14 recursion
14 recursion14 recursion
14 recursion
Himadri Sen Gupta
 
Recursion
RecursionRecursion
Recursion
Jesmin Akhter
 
WT-Pravesh Sakhare.pptx
WT-Pravesh Sakhare.pptxWT-Pravesh Sakhare.pptx
WT-Pravesh Sakhare.pptx
TuleshwarGupta1
 
L16
L16L16
L16
FALLEE31188
 
Recursion DM
Recursion DMRecursion DM
Recursion DM
Rokonuzzaman Rony
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
recursion2
recursion2recursion2
recursion2
Mohamed Elsayed
 
recursion, syntax, types, example program
recursion, syntax,  types, example programrecursion, syntax,  types, example program
recursion, syntax, types, example program
letheyabala
 
3-Recursive Function in programming .pptx
3-Recursive Function in programming .pptx3-Recursive Function in programming .pptx
3-Recursive Function in programming .pptx
naushigrdcs
 
Simple program recursion problem solving.pptx
Simple program recursion problem solving.pptxSimple program recursion problem solving.pptx
Simple program recursion problem solving.pptx
swethab129
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
Lakshmi Sarvani Videla
 
PyOhio Recursion Slides
PyOhio Recursion SlidesPyOhio Recursion Slides
PyOhio Recursion Slides
Rinita Gulliani
 
Lec-32 Recursion -Recursion in Computer Science
Lec-32 Recursion -Recursion in Computer ScienceLec-32 Recursion -Recursion in Computer Science
Lec-32 Recursion -Recursion in Computer Science
Anil Yadav
 
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program CorrectnessCMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
allyn joy calcaben
 
Lec-6 Recursion of Data Structures & Algorithms
Lec-6 Recursion of Data Structures & AlgorithmsLec-6 Recursion of Data Structures & Algorithms
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
recursion, syntax, types, example program
recursion, syntax,  types, example programrecursion, syntax,  types, example program
recursion, syntax, types, example program
letheyabala
 
3-Recursive Function in programming .pptx
3-Recursive Function in programming .pptx3-Recursive Function in programming .pptx
3-Recursive Function in programming .pptx
naushigrdcs
 
Simple program recursion problem solving.pptx
Simple program recursion problem solving.pptxSimple program recursion problem solving.pptx
Simple program recursion problem solving.pptx
swethab129
 

More from Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
Lakshmi Sarvani Videla
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
Lakshmi Sarvani Videla
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
Lakshmi Sarvani Videla
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
Lakshmi Sarvani Videla
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
Lakshmi Sarvani Videla
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
Lakshmi Sarvani Videla
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Lakshmi Sarvani Videla
 
Functions
FunctionsFunctions
Functions
Lakshmi Sarvani Videla
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
Lakshmi Sarvani Videla
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
Lakshmi Sarvani Videla
 
Graphs
GraphsGraphs
Graphs
Lakshmi Sarvani Videla
 
B trees
B treesB trees
B trees
Lakshmi Sarvani Videla
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
Lakshmi Sarvani Videla
 
Dictionary
DictionaryDictionary
Dictionary
Lakshmi Sarvani Videla
 
Sets
SetsSets
Sets
Lakshmi Sarvani Videla
 
Lists
ListsLists
Lists
Lakshmi Sarvani Videla
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
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
Lakshmi Sarvani Videla
 
C programs
C programsC programs
C programs
Lakshmi Sarvani Videla
 
Data Mining: Data Preprocessing
Data Mining: Data PreprocessingData Mining: Data Preprocessing
Data Mining: Data Preprocessing
Lakshmi Sarvani Videla
 

Recently uploaded (20)

A Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment GatewayA Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment Gateway
danielle hunter
 
System Card: Claude Opus 4 & Claude Sonnet 4
System Card: Claude Opus 4 & Claude Sonnet 4System Card: Claude Opus 4 & Claude Sonnet 4
System Card: Claude Opus 4 & Claude Sonnet 4
Razin Mustafiz
 
The fundamental misunderstanding in Team Topologies
The fundamental misunderstanding in Team TopologiesThe fundamental misunderstanding in Team Topologies
The fundamental misunderstanding in Team Topologies
Patricia Aas
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Eugene Fidelin
 
Reducing Bugs With Static Code Analysis php tek 2025
Reducing Bugs With Static Code Analysis php tek 2025Reducing Bugs With Static Code Analysis php tek 2025
Reducing Bugs With Static Code Analysis php tek 2025
Scott Keck-Warren
 
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
SOFTTECHHUB
 
AI against disinformation and why it is not enough
AI against disinformation and why it is not enoughAI against disinformation and why it is not enough
AI against disinformation and why it is not enough
Yiannis Kompatsiaris
 
Fully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and ControlFully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and Control
ShapeBlue
 
John Carmack’s Notes From His Upper Bound 2025 Talk
John Carmack’s Notes From His Upper Bound 2025 TalkJohn Carmack’s Notes From His Upper Bound 2025 Talk
John Carmack’s Notes From His Upper Bound 2025 Talk
Razin Mustafiz
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
SAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AI
SAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AISAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AI
SAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AI
Peter Spielvogel
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Content and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-TrainingContent and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-Training
Rustici Software
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Optimize IBM i with Consulting Services Help
Optimize IBM i with Consulting Services HelpOptimize IBM i with Consulting Services Help
Optimize IBM i with Consulting Services Help
Alice Gray
 
John Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 TalkJohn Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 Talk
Razin Mustafiz
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AIAI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
Buhake Sindi
 
A Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment GatewayA Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment Gateway
danielle hunter
 
System Card: Claude Opus 4 & Claude Sonnet 4
System Card: Claude Opus 4 & Claude Sonnet 4System Card: Claude Opus 4 & Claude Sonnet 4
System Card: Claude Opus 4 & Claude Sonnet 4
Razin Mustafiz
 
The fundamental misunderstanding in Team Topologies
The fundamental misunderstanding in Team TopologiesThe fundamental misunderstanding in Team Topologies
The fundamental misunderstanding in Team Topologies
Patricia Aas
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Marko.js - Unsung Hero of Scalable Web Frameworks (DevDays 2025)
Eugene Fidelin
 
Reducing Bugs With Static Code Analysis php tek 2025
Reducing Bugs With Static Code Analysis php tek 2025Reducing Bugs With Static Code Analysis php tek 2025
Reducing Bugs With Static Code Analysis php tek 2025
Scott Keck-Warren
 
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
SOFTTECHHUB
 
AI against disinformation and why it is not enough
AI against disinformation and why it is not enoughAI against disinformation and why it is not enough
AI against disinformation and why it is not enough
Yiannis Kompatsiaris
 
Fully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and ControlFully Open-Source Private Clouds: Freedom, Security, and Control
Fully Open-Source Private Clouds: Freedom, Security, and Control
ShapeBlue
 
John Carmack’s Notes From His Upper Bound 2025 Talk
John Carmack’s Notes From His Upper Bound 2025 TalkJohn Carmack’s Notes From His Upper Bound 2025 Talk
John Carmack’s Notes From His Upper Bound 2025 Talk
Razin Mustafiz
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
SAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AI
SAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AISAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AI
SAP Sapphire 2025 ERP1612 Enhancing User Experience with SAP Fiori and AI
Peter Spielvogel
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Content and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-TrainingContent and eLearning Standards: Finding the Best Fit for Your-Training
Content and eLearning Standards: Finding the Best Fit for Your-Training
Rustici Software
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Optimize IBM i with Consulting Services Help
Optimize IBM i with Consulting Services HelpOptimize IBM i with Consulting Services Help
Optimize IBM i with Consulting Services Help
Alice Gray
 
John Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 TalkJohn Carmack’s Slides From His Upper Bound 2025 Talk
John Carmack’s Slides From His Upper Bound 2025 Talk
Razin Mustafiz
 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
 
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AIAI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
Buhake Sindi
 

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 [email protected]
  • 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 [email protected] 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 [email protected] 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 [email protected] 𝑠𝑢𝑚 𝑛 = 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); }