0% found this document useful (0 votes)
10 views6 pages

Understanding Recursion in C Programming

The document explains recursion in programming, specifically in the C language, highlighting its definition and the importance of defining an exit condition to avoid infinite loops. It provides examples of recursive functions for calculating factorials, printing digits, and summing integers. The document emphasizes the utility of recursion in solving mathematical problems.

Uploaded by

ahnafatif87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Understanding Recursion in C Programming

The document explains recursion in programming, specifically in the C language, highlighting its definition and the importance of defining an exit condition to avoid infinite loops. It provides examples of recursive functions for calculating factorials, printing digits, and summing integers. The document emphasizes the utility of recursion in solving mathematical problems.

Uploaded by

ahnafatif87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

RECURSION

Bijan Paul
Senior Lecturer
Department of CSE
University of Liberal Arts Bangladesh
C - RECURSION
• Recursion is the process of repeating items in a self-similar way.

• In programming languages, if a program allows you to call a function inside the same
function, then it is called a recursive call of the function.

void recursion()
{
recursion(); /* function calls itself */

}
int main() {

recursion();

• The C programming language supports recursion, i.e., a function to call itself.

• But while using recursion, programmers need to be careful to define an exit condition
from the function, otherwise it will go into an infinite loop.
• Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc
Example: Number Factorial using Recursion
#include <stdio.h>

int fact( int i)


{

if(i <= 1) // exit condition


{
return 1;
}
return i * fact(i - 1);
}

int main() {
int i = 5;
printf("Factorial of %d is %d\n", i, fact(i));
return 0;
}

Output: Factorial of 5 is 120


Another Example 2 3

main main
#include <stdio.h> 1 &main 123
&main 123
void PrintDigit(int n)
main
{
&main 123
if (n / 10)
PrintDigit n PrintDigit n
PrintDigit(n / 10);
&PrintDigit 123 &PrintDigit 123

printf("%d\n", n % 10); PrintDigit n n/10=123/10=12 n/10=123/10=12


} &PrintDigit 123

PrintDigit n PrintDigit n
int main (void) &PrintDigit 12 &PrintDigit 12
{ n/10=12/1=1
PrintDigit( 123 ); 4
5
return 0; main
main PrintDigit n
} &main 123
&main 123 &PrintDigit 1
6
main
&main 123
PrintDigit n
PrintDigit n
&PrintDigit 123
&PrintDigit 123
Output n/10=123/10=12
PrintDigit n
1 &PrintDigit 123
2 Print(n%10=123%10=) 3 PrintDigit n
PrintDigit n
3 &PrintDigit 12
&PrintDigit 12

Print(n%10=12%10=) 2

PrintDigit n
&PrintDigit 1
Print(n%10=1%10=)1
Another Example
#include <stdio.h>
int Sum( int n )
{ For better visualization of this recursion:
if( n==0 ) Considering num = 5;
return n; sum(5)
else =5+sum(4)
return n+Sum(n-1); //self call =5+4+sum(3)
=5+4+3+sum(2)
}
=5+4+3+2+sum(1) =5+4+3+2+1+sum(0)
void main( void ){ =5+4+3+2+1+0
int num, add; =5+4+3+2+1
printf("Enter a positive integer:\n"); =5+4+3+3
=5+4+6
scanf("%d", &num); =5+10
add = Sum( num ); =15
printf("sum=%d", add);
}

Output: sum= 15
End

You might also like