Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
288 views

C Programming Recursion

The document discusses recursion in C programming. It provides an example program that uses recursion to calculate the sum of the first n natural numbers. The program defines a recursive sum function that calls itself, decreasing the argument by 1 each time, until the argument reaches 0, at which point it returns the value to end the recursion. A step-by-step breakdown is provided to visualize how the recursive calls build up to calculate the final sum. The document concludes by noting that recursion allows for cleaner code by dividing problems into sub-problems, but it can be difficult to think through and debug recursive logic.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
288 views

C Programming Recursion

The document discusses recursion in C programming. It provides an example program that uses recursion to calculate the sum of the first n natural numbers. The program defines a recursive sum function that calls itself, decreasing the argument by 1 each time, until the argument reaches 0, at which point it returns the value to end the recursion. A step-by-step breakdown is provided to visualize how the recursive calls build up to calculate the final sum. The document concludes by noting that recursion allows for cleaner code by dividing problems into sub-problems, but it can be difficult to think through and debug recursive logic.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Programming Recursion

A function that calls itself is known as recursive function and this technique is known as recursion in C
programming.

Example of recursion in C programming


Write a C program to find sum of first n natural numbers using recursion. Note: Positive integers are
known as natural number i.e. 1, 2, 3....n
#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}

Output
Enter a positive integer:
5
15

In, this simple C program, sum() function is invoked from the same function. If n is not equal to 0 then, the
function calls itself passing argument 1 less than the previous argument it was called with. Suppose, n is 5
initially. Then, during next function calls, 4 is passed to function and the value of argument decreases by 1 in
each recursive call. When, n becomes equal to 0, the value of n is returned which is the sum numbers from 5 to
1.
For better visualization of recursion in this example:
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0

=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15

Every recursive function must be provided with a way to end the recursion. In this example when, n is equal to
0, there is no recursive call and recursion ends.

Advantages and Disadvantages of Recursion


Recursion is more elegant and requires few variables which make program clean. Recursion can be used to
replace complex nesting code by dividing the problem into same problem of its sub -type.
In other hand, it is hard to think the logic of a recursive function. It is also difficult to debug the code
containing recursion.

You might also like