SlideShare a Scribd company logo
Recursion
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
12
1
Outline
 What is Recursion?
 How Recursion works?
 Examples of Recursion
 Advantages and Disadvantages of Recursion
What is Recursion?
 A function that calls itself is known as a recursive
function.This technique is known as recursion.
How Recursion works?
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
How Recursion works? (cont..)
How Recursion works? (cont..)
 The recursion continues until some condition is met
to prevent it.
 To prevent infinite recursion, if...else statement (or
similar approach) can be used where one branch
makes the recursive call and other doesn't.
Recursion: Example1
// Find the Sum of Natural Numbers using Recursion
#include <stdio.h>
int sum(int n); // function prototype
int main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
}
Recursion: Example1 (continue)
int sum(int num)
{
if (num!=0)
return num + sum(num-1); // sum() function calls itself
else
return num;
}
Output:
Enter a positive integer:3
sum = 6
Recursion: Example1 (continue)
Recursion: Example2
// Find Factorial of a Number Using Recursion
#include <stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
Recursion: Example2 (cont..)
long int multiplyNumbers(int n)
{
if (n >= 1)
return n * multiplyNumbers(n-1);
else
return 1;
}
Output:
Enter a positive integer: 6
Factorial of 6 = 720
Recursion: Example2 explaination
 Suppose the user entered 6.
 Initially, the multiplyNumbers() is called from the main()
function with 6 passed as an argument.
 Then, 5 is passed to the multiplyNumbers() function from the
same function (recursive call). In each recursive call, the value
of argument n is decreased by 1.
 When the value of n is less than 1, there is no recursive call.
Recursion: Example2 (explanation)
 Suppose the user entered 6.
 Initially, the multiplyNumbers() is called from the main()
function with 6 passed as an argument.
 Then, 5 is passed to the multiplyNumbers() function from the
same function (recursive call). In each recursive call, the value
of argument n is decreased by 1.
 When the value of n is less than 1, there is no recursive call.
Recursion: Example3
// GCD of Two Numbers using Recursion
#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2));
return 0;
}
Recursion: Example3 (cont…)
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2);
else
return n1;
}
Output:
Enter two positive integers: 366 60
G.C.D of 366 and 60 is 6.
Advantages and Disadvantages of Recursion
Advantages:
1. Reduce unnecessary calling of function.
2. Through Recursion one can Solve problems in easy way while its iterative
solution is very big and complex.
Disadvantages:
3. Recursive solution is always logical and it is very difficult to trace. (debug and
understand).
4. In recursive we must have an if statement somewhere to force the function to
return without the recursive call being executed, otherwise the function will
never return.
5. Recursion takes a lot of stack space, usually not considerable when the program
is small and running on a PC.
6. Recursion uses more processor time.
Lecture 12 - Recursion

More Related Content

PPTX
concept of Array, 1D & 2D array
PPTX
Dynamic memory allocation
PPTX
Function in c
PPT
Strings in c
PPTX
Functions in c
PPTX
Function overloading
PDF
Function in C
PPTX
16 dynamic-memory-allocation
concept of Array, 1D & 2D array
Dynamic memory allocation
Function in c
Strings in c
Functions in c
Function overloading
Function in C
16 dynamic-memory-allocation

What's hot (20)

PPTX
C Programming: Control Structure
PPTX
PPT
Two dimensional array
PPTX
C Structures and Unions
PPTX
Union in C programming
PPTX
C programing -Structure
PPTX
Pointers in c language
PDF
Arrays in C++
PPTX
Functions in c++
PPT
Strings Functions in C Programming
PDF
C Pointers
PPTX
File in C language
PPSX
5bit field
PPTX
Pointers in c++
PPT
Infix to Postfix Conversion Using Stack
PPTX
Pointer arithmetic in c
PPT
Function overloading(c++)
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPTX
Constructor and Types of Constructors
PPTX
inline function
C Programming: Control Structure
Two dimensional array
C Structures and Unions
Union in C programming
C programing -Structure
Pointers in c language
Arrays in C++
Functions in c++
Strings Functions in C Programming
C Pointers
File in C language
5bit field
Pointers in c++
Infix to Postfix Conversion Using Stack
Pointer arithmetic in c
Function overloading(c++)
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Constructor and Types of Constructors
inline function
Ad

Similar to Lecture 12 - Recursion (20)

PDF
Recursion
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
PPT
lecture 10 Recursive Function and Macros.ppt
PPTX
recursion, syntax, types, example program
PPTX
3-Recursive Function in programming .pptx
PPTX
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
PPTX
06 Recursion in C.pptx
PPT
14 recursion
PDF
7 functions
PPTX
Recursive Function
PDF
[ITP - Lecture 14] Recursion
PPT
Data Structures- Part5 recursion
DOCX
Recursion in C
PPTX
recursion_esccccccccccc1111122222333.pptx
PPTX
Recursion c programming.pptx
PPTX
Recursion cps.pptx
PPTX
ECE2102-Week10-11-Recursion-Conclusion.pptx
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PDF
Recursion.pdf
PDF
Recursion concepts by Divya
Recursion
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
lecture 10 Recursive Function and Macros.ppt
recursion, syntax, types, example program
3-Recursive Function in programming .pptx
nebnznsnshsjsjsjsjsjssjsjsjsjsjsjsjdjw 2.pptx
06 Recursion in C.pptx
14 recursion
7 functions
Recursive Function
[ITP - Lecture 14] Recursion
Data Structures- Part5 recursion
Recursion in C
recursion_esccccccccccc1111122222333.pptx
Recursion c programming.pptx
Recursion cps.pptx
ECE2102-Week10-11-Recursion-Conclusion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Recursion.pdf
Recursion concepts by Divya
Ad

More from Md. Imran Hossain Showrov (20)

PPT
Lecture 22 - Error Handling
PPT
Lecture 21 - Preprocessor and Header File
PPT
Lecture 20 - File Handling
PPT
Lecture 19 - Struct and Union
PPT
Lecture 18 - Pointers
PPT
Lecture 16 - Multi dimensional Array
PPT
Lecture 17 - Strings
PPT
Lecture 15 - Array
PPT
Lecture 14 - Scope Rules
PPT
Lecture 13 - Storage Classes
PPT
Lecture 11 - Functions
PPT
Lecture 10 - Control Structures 2
PPT
Lecture 8- Data Input and Output
PPT
Lecture 9- Control Structures 1
PPT
Lecture 7- Operators and Expressions
PPT
Lecture 6- Intorduction to C Programming
PPT
Lecture 5 - Structured Programming Language
PPT
Lecture 4- Computer Software and Languages
PPT
Lecture 3 - Processors, Memory and I/O devices
PPT
Lecture 2 - Introductory Concepts
Lecture 22 - Error Handling
Lecture 21 - Preprocessor and Header File
Lecture 20 - File Handling
Lecture 19 - Struct and Union
Lecture 18 - Pointers
Lecture 16 - Multi dimensional Array
Lecture 17 - Strings
Lecture 15 - Array
Lecture 14 - Scope Rules
Lecture 13 - Storage Classes
Lecture 11 - Functions
Lecture 10 - Control Structures 2
Lecture 8- Data Input and Output
Lecture 9- Control Structures 1
Lecture 7- Operators and Expressions
Lecture 6- Intorduction to C Programming
Lecture 5 - Structured Programming Language
Lecture 4- Computer Software and Languages
Lecture 3 - Processors, Memory and I/O devices
Lecture 2 - Introductory Concepts

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Sports Quiz easy sports quiz sports quiz
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
PPH.pptx obstetrics and gynecology in nursing
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Anesthesia in Laparoscopic Surgery in India
Sports Quiz easy sports quiz sports quiz
01-Introduction-to-Information-Management.pdf
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Basic Mud Logging Guide for educational purpose
TR - Agricultural Crops Production NC III.pdf
RMMM.pdf make it easy to upload and study
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Lecture 12 - Recursion

  • 1. Recursion Md. Imran Hossain Showrov (showrovsworld@gmail.com) 12 1
  • 2. Outline  What is Recursion?  How Recursion works?  Examples of Recursion  Advantages and Disadvantages of Recursion
  • 3. What is Recursion?  A function that calls itself is known as a recursive function.This technique is known as recursion.
  • 4. How Recursion works? void recurse() { ... .. ... recurse(); ... .. ... } int main() { ... .. ... recurse(); ... .. ... }
  • 6. How Recursion works? (cont..)  The recursion continues until some condition is met to prevent it.  To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.
  • 7. Recursion: Example1 // Find the Sum of Natural Numbers using Recursion #include <stdio.h> int sum(int n); // function prototype int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum = %d", result); }
  • 8. Recursion: Example1 (continue) int sum(int num) { if (num!=0) return num + sum(num-1); // sum() function calls itself else return num; } Output: Enter a positive integer:3 sum = 6
  • 10. Recursion: Example2 // Find Factorial of a Number Using Recursion #include <stdio.h> long int multiplyNumbers(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %ld", n, multiplyNumbers(n)); return 0; }
  • 11. Recursion: Example2 (cont..) long int multiplyNumbers(int n) { if (n >= 1) return n * multiplyNumbers(n-1); else return 1; } Output: Enter a positive integer: 6 Factorial of 6 = 720
  • 12. Recursion: Example2 explaination  Suppose the user entered 6.  Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.  Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.  When the value of n is less than 1, there is no recursive call.
  • 13. Recursion: Example2 (explanation)  Suppose the user entered 6.  Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.  Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.  When the value of n is less than 1, there is no recursive call.
  • 14. Recursion: Example3 // GCD of Two Numbers using Recursion #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2)); return 0; }
  • 15. Recursion: Example3 (cont…) int hcf(int n1, int n2) { if (n2 != 0) return hcf(n2, n1%n2); else return n1; } Output: Enter two positive integers: 366 60 G.C.D of 366 and 60 is 6.
  • 16. Advantages and Disadvantages of Recursion Advantages: 1. Reduce unnecessary calling of function. 2. Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex. Disadvantages: 3. Recursive solution is always logical and it is very difficult to trace. (debug and understand). 4. In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return. 5. Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC. 6. Recursion uses more processor time.