SlideShare a Scribd company logo
2
Most read
3
Most read
11
Most read
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

More Related Content

PPT
Recursion
PDF
Iterations and Recursions
PPT
Data Structures- Part5 recursion
PPT
Data Structures- Part2 analysis tools
PPT
Recursion - Algorithms and Data Structures
PPT
Ch10 Recursion
PPT
Data Structures- Part1 overview and review
PDF
design and analysis of algorithm
Recursion
Iterations and Recursions
Data Structures- Part5 recursion
Data Structures- Part2 analysis tools
Recursion - Algorithms and Data Structures
Ch10 Recursion
Data Structures- Part1 overview and review
design and analysis of algorithm

What's hot (20)

PPT
CS8451 - Design and Analysis of Algorithms
PPTX
Recursion and Sorting Algorithms
PPTX
Introduction to the AKS Primality Test
PPT
functions
PPT
Recursion and looping
PPTX
Recursion
PPTX
The Complexity Of Primality Testing
PDF
01. design & analysis of agorithm intro & complexity analysis
PDF
Introduction to Recursion (Python)
PPT
Numerical Methods
RTF
algorithm unit 1
PPT
Analysis Of Algorithms I
PDF
Design & Analysis Of Algorithm
PDF
PPTX
Divide and conquer
PPT
Divide and conquer
PDF
Network Security CS3-4
PDF
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
CS8451 - Design and Analysis of Algorithms
Recursion and Sorting Algorithms
Introduction to the AKS Primality Test
functions
Recursion and looping
Recursion
The Complexity Of Primality Testing
01. design & analysis of agorithm intro & complexity analysis
Introduction to Recursion (Python)
Numerical Methods
algorithm unit 1
Analysis Of Algorithms I
Design & Analysis Of Algorithm
Divide and conquer
Divide and conquer
Network Security CS3-4
QUESTION BANK FOR ANNA UNNIVERISTY SYLLABUS
Ad

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

PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
PPTX
Recursion
DOCX
Recursion in C
PDF
Recursion.pdf
PPTX
recursion_esccccccccccc1111122222333.pptx
PPT
Lec-6 Recursion of Data Structures & Algorithms
PDF
Iteration, induction, and recursion
PPTX
PPT
14 recursion
PDF
Programming in Java: Recursion
PPTX
Introduction to Dynamic Programming.pptx
PPTX
DS Unit-1.pptx very easy to understand..
PPTX
01 - DAA - PPT.pptx
PPTX
Recursion DS made easy - 2
PDF
1 chapter1 introduction
PPTX
Recursion part 2
PPTX
Recursion in Data Structure
PPT
recursion based on koffmann and wolfgang
PDF
Recursion For the Rest of Us (CS Fundamentals Series)
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
Recursion
Recursion in C
Recursion.pdf
recursion_esccccccccccc1111122222333.pptx
Lec-6 Recursion of Data Structures & Algorithms
Iteration, induction, and recursion
14 recursion
Programming in Java: Recursion
Introduction to Dynamic Programming.pptx
DS Unit-1.pptx very easy to understand..
01 - DAA - PPT.pptx
Recursion DS made easy - 2
1 chapter1 introduction
Recursion part 2
Recursion in Data Structure
recursion based on koffmann and wolfgang
Recursion For the Rest of Us (CS Fundamentals Series)
Ad

More from Sumit Pandey (12)

PPTX
Linear Regression | Machine Learning | Data Science
PPTX
EDA | Exploratory Data Analysis | Machine Learning | Data Science
PPTX
Conflicts | Professionalism | Business
PPTX
Social Media Writing | English
PPTX
Types of keys in database | SQL
PPTX
Kernel | Operating System
PPTX
Unix architecture | Operating System
PPTX
Simple past tense | English Grammar
PPTX
NETWORK SECURITY AND VIRUSES
PPTX
ERADICATION OF CORRUPTION IN GOVERNMENT ESTABLISHMENTS
PPT
ARTIFICIAL INTELLIGENCE
PPTX
GST - Good Service Tax
Linear Regression | Machine Learning | Data Science
EDA | Exploratory Data Analysis | Machine Learning | Data Science
Conflicts | Professionalism | Business
Social Media Writing | English
Types of keys in database | SQL
Kernel | Operating System
Unix architecture | Operating System
Simple past tense | English Grammar
NETWORK SECURITY AND VIRUSES
ERADICATION OF CORRUPTION IN GOVERNMENT ESTABLISHMENTS
ARTIFICIAL INTELLIGENCE
GST - Good Service Tax

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PDF
Computing-Curriculum for Schools in Ghana
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial disease of the cardiovascular and lymphatic systems
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Insiders guide to clinical Medicine.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
Computing-Curriculum for Schools in Ghana
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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