Recursion in c
Just go through the below program, which is to find whether the given number is Armstrong or not.
You must have seen this code written using looping statements.
But, the code can also be written using recursion.
Have a look at the function definition of the function "armstrong()".
Have a look in the line statement,
s=((n%10)*(n%10)*(n%10)+armstrong(n/10));
here you will see the function armstrong(n/10) is called in the body of the same function. An integer value of (n/10) is passed through the function.
This is called recursion.
Example:
#include <stdio.h> int armstrong(int); int main() { int num,temp=0; printf("Enter the number:"); scanf("%d",&num); temp=armstrong(num); if(num==temp) printf("yes, number is an Armstrong number"); else printf("No, number is not an Armstrong number"); return 0; } int armstrong(int n) { int s=0; if(n==0) return 0; else { s=((n%10)*(n%10)*(n%10)+armstrong(n/10)); } return s; }
How does recursion works?
Algorithm:
1. In the main function input "num" is taken.
2. A function declared "int armstrong(int)" is called and, the "num" value is passed through the function as an argument.
3. In the function definition, the value gets initialized to the parameter ( int n).
4. consider, n=153
The value enters into the body of function definition.
The condition is checked if(n==0), the condition becomes false, curser goes to else part.
In else part, An algorithm to calculate the armstrong is followed. The result will be stored in variable s.
The alogrithm is, s=((n%10)*(n%10)*(n%10)+armstrong(n/10)).
Let's see the flow of the algorithm.
Initially, n is 153.
n%10=153%10=3.
n/10=153/10=15.
s=((3*3*3)+armstrong(15)).
"armstrong" is the function that is called by the same function. This is called a recursive call.
The cycle will repeat from step 4. Now, the value of n is 15.
The cycle will continue until n is 0.
Observe the below figure. You will get an idea of how recursion works.
Stack plays an important role in recursion.
Use of stack in recursion.
Stack follows the rule of last in first out. We all know this about Stack, Then how is stack used in recursion?
In any program, whenever the variable is declared, it is stored in the stack. The variable stays in the stack until the program is terminated. If the variable is declared in the main function or, any other function, the life span of the variable is until the function gets terminated.
In recursion, whenever the function calls itself, the value of the previous call is stored in the stack. At every call, the value gets pushed into the stack. When the function returns the value, the value is executed and popped in the reverse order.
Push operation:
Pop operation:
Advantages and Disadvantage of recursion in c
3 Comments
Anyone having a keen interest in artificial intelligence which require analytical knowledge and want to contribute to these fields, MBA in Artificial Intelligence is definitely for you.
ReplyDeleteReally awesome article. Nice information. Informative and knowledgeable. Thanks for sharing this article with us. Keep sharing more.
ReplyDeleteAI Patasala Data Science Online Training
Slots Casino Games - Jtmhub
ReplyDeleteThe best slots casinos offer free play without registration 동두천 출장안마 and 보령 출장마사지 without deposit and no 광주광역 출장안마 deposit required. This 인천광역 출장샵 ensures that all the casinos are fair and 익산 출장마사지 fair for players
Post a Comment