C Programming Recursion - Programming Languages

Latest

Thursday 26 January 2017

C Programming Recursion

C Programming Recursion 

In this tutorial, we will learn to create a recursive function in C programming.

"A function that calls itself is known as a recursive function. And, this technique is known as recursion."

How recursion works?


void recurse()
{
    ... .. ...
    recurse();
    ... .. ...
}

int main()
{
    ... .. ...
    recurse();
    ... .. ...
}
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.
 

Example: Sum of Natural Numbers Using Recursion

#include <stdio.h>  
int sum(int n);  
int main()  
{  
int number, result;  
printf("Enter a positive integer: "); 
scanf("%d", &number); 
result = sum(number); 
printf("sum=%d", result); 
} 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
6

 Analysis of the above solved program:

Initially, the sum() is called from the main() function with number passed as an argument.

Suppose, the value of num is 3 initially. During next function call, 2 is passed to the sum() function. This process continues until num is equal to 0.
When num is equal to 0, the if condition fails and the else part is executed returning the sum of integers to the main() function.


Advantages and Disadvantages of Recursion
Recursion makes program elegant and cleaner. All algorithms can be defined recursively which makes it easier to visualize and prove. 
If the speed of the program is vital then, you should avoid using recursion. Recursions use more memory and are generally slow. Instead, you can use loop.

Example: Sum of Natural Numbers Using Recursion


#include <stdio.h>
int addNumbers(int n);

int main()
{
    int num;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    printf("Sum = %d",addNumbers(num));
    return 0;
}

int addNumbers(int n)
{
    if(n != 0)
        return n + addNumbers(n-1);
    else
        return n;
}
Output
Enter a positive integer: 20 
Sum = 210

Analysis of the above solved program:

Suppose the user entered 20.
Initially, the addNumbers() is called from the main() function with 20 passed as an argument.
The number (20) is added to the result of addNumbers(19).
In the next function call from addNumbers() to addNumbers(), 19 is passed which is added to the result of addNumbers(18). This process continues until n is equal to 0.
When n is equal to 0, there is no recursive call and this returns the sum of integers to the main() function.

Example: 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;
}

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.

Example: 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;
}
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 

Analysis of the above solved program:

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.

Thats all.We will come up again with new so keep learning and enjoying The life.

Thanks

 

 

 

No comments:

Post a Comment