SlideShare a Scribd company logo
P S D S - R E C U R S I O N
It Recurs.
By - Kanan,Kirti & Sumit
The process in which a function calls itself directly or
indirectly is called recursion and the corresponding function
is called as recursive function.
Using recursive algorithm, certain problems can be solved quite easily.
The function containing recursion is called recursive function, at the end
this is a great tool in the hand of the programmers to code some
problems in a lot easier and efficient way.
In the recursive program, the solution to the base
case is provided and the solution of the bigger problem is
expressed in terms of smaller problems.
The idea is to represent a problem in terms of one or more smaller
problems, and add one or more base conditions that stop the recursion.
For example, we compute factorial n if we know factorial of (n-1).
The base case for factorial would be n = 0. We return 1 when n = 0.
WHAT IS RECURSION ?
Property Recursion Iteration
Definition Function calls itself. A set of instructions repeatedly executed.
Application For functions. For loops.
Termination Through base case, where there will be no function call.
When the termination condition for the
iterator ceases to be satisfied.
Usage
Used when code size needs to be small, and time
complexity is not an issue.
Used when time complexity needs to be
balanced against an expanded code size.
Code Size Smaller code size Larger Code Size.
Time Complexity Very high(generally exponential) time complexity.
Relatively lower time complexity
(generally polynomial-logarithmic).
RECURSION VS ITERATION
The program ‘Sum of digits’ uses recursion and counts the
sum of first N natural number.
Consider a function existed as sum and if the user pass n
to it, it will return the sum of the number and now that we
have considered that already a function is made so whenever
we will call it, it will give the result as in this code.
The user enters the Nth number as the input, the
program then calculates the sum of first N numbers using recursion
and then displays the final result.
And when the n is not equal to 0, there is no recursive call.
This returns sum of digits ultimately to the main function.
SUM TILL ‘N’ – ALGORITHM
Sum Till ‘N’ – C Code
Output : 10
For finding the sum of digits, we need to find all digits and sum all of them.
Finding sum of digits includes three basic steps :
1. Find last digit of number using modular division by 10.
2. Add the last digit found above to sum variable.
3. Remove last digit from given number by dividing it by 10.
Algorithm :
1. Get the number
2. Declare a variable to store the sum and set it to 0.
3. Repeat the next two steps, till the number is not 0.
4. Get the rightmost digit of the number with help of the remainder
‘%’ operator by dividing it by 10 and add it to sum.
5. Divide the number by 10 with help of '/' operator to remove
the rightmost digit.
6. Print or return the sum
SUM OF DIGITS – ALGORITHM
Sum Of Digits – C Code
Output : 4
A Prime number is a number that is divisible only by itself and 1.
For example – 3, 5, 7, 11 etc.
To check if a number is divisible only by 1 and itself, we put every number
greater than 1 and less than the number itself in place of the divisor,
and if we find that the number is divisible by any of them, we break the
loop, and return false, otherwise it is true.
As the above method will not fall under the time constraints, we use
Recursion, in recursion, we check the base cases every single time,
until our condition falls into it, otherwise we increase the value of our divisor
which obviously starts from 2.
Check Prime Or Not – ALGORITHM
Check Prime Or Not – C Code
Output : Yes
Recursion | C++ | DSA
Recursion | C++ | DSA
Ad

More Related Content

What's hot (20)

CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality Test
Pranshu Bhatnagar
 
Primality
PrimalityPrimality
Primality
Mohanasundaram Nattudurai
 
functions
 functions  functions
functions
Gaditek
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
xcoolanurag
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
The Complexity Of Primality Testing
The Complexity Of Primality TestingThe Complexity Of Primality Testing
The Complexity Of Primality Testing
Mohammad Elsheikh
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
Onkar Nath Sharma
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
ESUG
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
Monika Choudhery
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
Sri Prasanna
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
Function
Function Function
Function
QamarAbbasJaffari
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
ramya marichamy
 
Recursion
RecursionRecursion
Recursion
baabtra.com - No. 1 supplier of quality freshers
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Network Security CS3-4
Network Security CS3-4 Network Security CS3-4
Network Security CS3-4
Infinity Tech Solutions
 
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUSQUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
JAMBIKA
 

Similar to Recursion | C++ | DSA (20)

Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
R.K.College of engg & Tech
 
Python Programming Introduction - Loops & Boolean
Python Programming Introduction  - Loops & BooleanPython Programming Introduction  - Loops & Boolean
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
Robert Geofroy
 
Presentation 1 on algorithm for lab progress
Presentation 1 on algorithm for lab progressPresentation 1 on algorithm for lab progress
Presentation 1 on algorithm for lab progress
manjudarshan8543
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Lecture 7.pptx
Lecture 7.pptxLecture 7.pptx
Lecture 7.pptx
Arul Jothi Yuvaraja
 
L06
L06L06
L06
Saurav Rahman
 
C code examples
C code examplesC code examples
C code examples
Industrial Electric, Electronic Ind. And Trade Co. Ltd.
 
Data structures Lecture no. 4
Data structures Lecture no. 4Data structures Lecture no. 4
Data structures Lecture no. 4
AzharIqbal710687
 
3.3 - The Math Object.pptx
3.3 - The Math Object.pptx3.3 - The Math Object.pptx
3.3 - The Math Object.pptx
MaryhesterDeleon
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math object
allenbailey
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
Lecture10(Repetition-Part 1) computers.pdf
Lecture10(Repetition-Part 1) computers.pdfLecture10(Repetition-Part 1) computers.pdf
Lecture10(Repetition-Part 1) computers.pdf
jayyusimagdaong24
 
Problem Solving PPT Slides Grade 10- 11students
Problem Solving PPT Slides Grade 10- 11studentsProblem Solving PPT Slides Grade 10- 11students
Problem Solving PPT Slides Grade 10- 11students
chamm5
 
03b loops
03b   loops03b   loops
03b loops
Manzoor ALam
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
HARSHSHARMA840
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
universidad industrial de santander
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
universidad industrial de santander
 
Algorithm types performance steps working
Algorithm types performance steps workingAlgorithm types performance steps working
Algorithm types performance steps working
Saurabh846965
 
Python Programming Introduction - Loops & Boolean
Python Programming Introduction  - Loops & BooleanPython Programming Introduction  - Loops & Boolean
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
Robert Geofroy
 
Presentation 1 on algorithm for lab progress
Presentation 1 on algorithm for lab progressPresentation 1 on algorithm for lab progress
Presentation 1 on algorithm for lab progress
manjudarshan8543
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Data structures Lecture no. 4
Data structures Lecture no. 4Data structures Lecture no. 4
Data structures Lecture no. 4
AzharIqbal710687
 
3.3 - The Math Object.pptx
3.3 - The Math Object.pptx3.3 - The Math Object.pptx
3.3 - The Math Object.pptx
MaryhesterDeleon
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math object
allenbailey
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
cogaxor346
 
Lecture10(Repetition-Part 1) computers.pdf
Lecture10(Repetition-Part 1) computers.pdfLecture10(Repetition-Part 1) computers.pdf
Lecture10(Repetition-Part 1) computers.pdf
jayyusimagdaong24
 
Problem Solving PPT Slides Grade 10- 11students
Problem Solving PPT Slides Grade 10- 11studentsProblem Solving PPT Slides Grade 10- 11students
Problem Solving PPT Slides Grade 10- 11students
chamm5
 
Algorithm types performance steps working
Algorithm types performance steps workingAlgorithm types performance steps working
Algorithm types performance steps working
Saurabh846965
 
Ad

More from Sumit Pandey (12)

Linear Regression | Machine Learning | Data Science
Linear Regression | Machine Learning | Data ScienceLinear Regression | Machine Learning | Data Science
Linear Regression | Machine Learning | Data Science
Sumit Pandey
 
EDA | Exploratory Data Analysis | Machine Learning | Data Science
EDA | Exploratory Data Analysis | Machine Learning | Data ScienceEDA | Exploratory Data Analysis | Machine Learning | Data Science
EDA | Exploratory Data Analysis | Machine Learning | Data Science
Sumit Pandey
 
Conflicts | Professionalism | Business
Conflicts | Professionalism | BusinessConflicts | Professionalism | Business
Conflicts | Professionalism | Business
Sumit Pandey
 
Social Media Writing | English
Social Media Writing | EnglishSocial Media Writing | English
Social Media Writing | English
Sumit Pandey
 
Types of keys in database | SQL
Types of keys in database | SQLTypes of keys in database | SQL
Types of keys in database | SQL
Sumit Pandey
 
Kernel | Operating System
Kernel | Operating SystemKernel | Operating System
Kernel | Operating System
Sumit Pandey
 
Unix architecture | Operating System
Unix architecture | Operating SystemUnix architecture | Operating System
Unix architecture | Operating System
Sumit Pandey
 
Simple past tense | English Grammar
Simple past tense | English GrammarSimple past tense | English Grammar
Simple past tense | English Grammar
Sumit Pandey
 
NETWORK SECURITY AND VIRUSES
NETWORK SECURITY AND VIRUSESNETWORK SECURITY AND VIRUSES
NETWORK SECURITY AND VIRUSES
Sumit Pandey
 
ERADICATION OF CORRUPTION IN GOVERNMENT ESTABLISHMENTS
ERADICATION OF CORRUPTION IN  GOVERNMENT ESTABLISHMENTSERADICATION OF CORRUPTION IN  GOVERNMENT ESTABLISHMENTS
ERADICATION OF CORRUPTION IN GOVERNMENT ESTABLISHMENTS
Sumit Pandey
 
ARTIFICIAL INTELLIGENCE
ARTIFICIAL INTELLIGENCEARTIFICIAL INTELLIGENCE
ARTIFICIAL INTELLIGENCE
Sumit Pandey
 
GST - Good Service Tax
GST - Good Service TaxGST - Good Service Tax
GST - Good Service Tax
Sumit Pandey
 
Linear Regression | Machine Learning | Data Science
Linear Regression | Machine Learning | Data ScienceLinear Regression | Machine Learning | Data Science
Linear Regression | Machine Learning | Data Science
Sumit Pandey
 
EDA | Exploratory Data Analysis | Machine Learning | Data Science
EDA | Exploratory Data Analysis | Machine Learning | Data ScienceEDA | Exploratory Data Analysis | Machine Learning | Data Science
EDA | Exploratory Data Analysis | Machine Learning | Data Science
Sumit Pandey
 
Conflicts | Professionalism | Business
Conflicts | Professionalism | BusinessConflicts | Professionalism | Business
Conflicts | Professionalism | Business
Sumit Pandey
 
Social Media Writing | English
Social Media Writing | EnglishSocial Media Writing | English
Social Media Writing | English
Sumit Pandey
 
Types of keys in database | SQL
Types of keys in database | SQLTypes of keys in database | SQL
Types of keys in database | SQL
Sumit Pandey
 
Kernel | Operating System
Kernel | Operating SystemKernel | Operating System
Kernel | Operating System
Sumit Pandey
 
Unix architecture | Operating System
Unix architecture | Operating SystemUnix architecture | Operating System
Unix architecture | Operating System
Sumit Pandey
 
Simple past tense | English Grammar
Simple past tense | English GrammarSimple past tense | English Grammar
Simple past tense | English Grammar
Sumit Pandey
 
NETWORK SECURITY AND VIRUSES
NETWORK SECURITY AND VIRUSESNETWORK SECURITY AND VIRUSES
NETWORK SECURITY AND VIRUSES
Sumit Pandey
 
ERADICATION OF CORRUPTION IN GOVERNMENT ESTABLISHMENTS
ERADICATION OF CORRUPTION IN  GOVERNMENT ESTABLISHMENTSERADICATION OF CORRUPTION IN  GOVERNMENT ESTABLISHMENTS
ERADICATION OF CORRUPTION IN GOVERNMENT ESTABLISHMENTS
Sumit Pandey
 
ARTIFICIAL INTELLIGENCE
ARTIFICIAL INTELLIGENCEARTIFICIAL INTELLIGENCE
ARTIFICIAL INTELLIGENCE
Sumit Pandey
 
GST - Good Service Tax
GST - Good Service TaxGST - Good Service Tax
GST - Good Service Tax
Sumit Pandey
 
Ad

Recently uploaded (20)

pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Rococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th centuryRococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th century
Gema
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdfRanking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Rafael Villas B
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Lecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptxLecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptx
Arshad Shaikh
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Rococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th centuryRococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th century
Gema
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdfRanking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Rafael Villas B
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Lecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptxLecture 1 Introduction history and institutes of entomology_1.pptx
Lecture 1 Introduction history and institutes of entomology_1.pptx
Arshad Shaikh
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 

Recursion | C++ | DSA

  • 1. P S D S - R E C U R S I O N It Recurs. By - Kanan,Kirti & Sumit
  • 2. The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. The function containing recursion is called recursive function, at the end this is a great tool in the hand of the programmers to code some problems in a lot easier and efficient way. In the recursive program, the solution to the base case is provided and the solution of the bigger problem is expressed in terms of smaller problems. The idea is to represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion. For example, we compute factorial n if we know factorial of (n-1). The base case for factorial would be n = 0. We return 1 when n = 0. WHAT IS RECURSION ?
  • 3. Property Recursion Iteration Definition Function calls itself. A set of instructions repeatedly executed. Application For functions. For loops. Termination Through base case, where there will be no function call. When the termination condition for the iterator ceases to be satisfied. Usage Used when code size needs to be small, and time complexity is not an issue. Used when time complexity needs to be balanced against an expanded code size. Code Size Smaller code size Larger Code Size. Time Complexity Very high(generally exponential) time complexity. Relatively lower time complexity (generally polynomial-logarithmic). RECURSION VS ITERATION
  • 4. The program ‘Sum of digits’ uses recursion and counts the sum of first N natural number. Consider a function existed as sum and if the user pass n to it, it will return the sum of the number and now that we have considered that already a function is made so whenever we will call it, it will give the result as in this code. The user enters the Nth number as the input, the program then calculates the sum of first N numbers using recursion and then displays the final result. And when the n is not equal to 0, there is no recursive call. This returns sum of digits ultimately to the main function. SUM TILL ‘N’ – ALGORITHM
  • 5. Sum Till ‘N’ – C Code Output : 10
  • 6. For finding the sum of digits, we need to find all digits and sum all of them. Finding sum of digits includes three basic steps : 1. Find last digit of number using modular division by 10. 2. Add the last digit found above to sum variable. 3. Remove last digit from given number by dividing it by 10. Algorithm : 1. Get the number 2. Declare a variable to store the sum and set it to 0. 3. Repeat the next two steps, till the number is not 0. 4. Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and add it to sum. 5. Divide the number by 10 with help of '/' operator to remove the rightmost digit. 6. Print or return the sum SUM OF DIGITS – ALGORITHM
  • 7. Sum Of Digits – C Code Output : 4
  • 8. A Prime number is a number that is divisible only by itself and 1. For example – 3, 5, 7, 11 etc. To check if a number is divisible only by 1 and itself, we put every number greater than 1 and less than the number itself in place of the divisor, and if we find that the number is divisible by any of them, we break the loop, and return false, otherwise it is true. As the above method will not fall under the time constraints, we use Recursion, in recursion, we check the base cases every single time, until our condition falls into it, otherwise we increase the value of our divisor which obviously starts from 2. Check Prime Or Not – ALGORITHM
  • 9. Check Prime Or Not – C Code Output : Yes

Editor's Notes

  • #2: Kanan
  • #3: Kirti
  • #4: Sumit A program is called recursive when an entity calls itself. A program is call iterative when there is a loop (or repetition).
  • #5: Kana
  • #6: kanan
  • #7: Kirti
  • #8: kirti
  • #9: Sumit
  • #10: Sumit
  • #11: Kanan
  • #12: Everyone