Recursion , Recursive Algorithm , Use of Recursion , Recursion in C Programme . If something is called by itself, then it is recurrence / recursion. For example, the picture in that photo, that image is again inside the picture … the process is a liberalization of Rikorsana.

If you call yourself at an empty field or call in your name in a huge room, you can feel the recurrence.

See the picture below, it is also a recurrence.

From Wikipedia

Please search by typing Recursion in Google. Will be written repeatedly Did you mean: Recursion. After the spell is correct, it will show. Google has made a rickshaw on the recursion search term.

Recursive Algorithm is the algorithm that calls itself, it’s Recruitment is used in a program using a recursive algorithm in computer programming. Recording is applied by calling itself a function in different programming languages. We will use two recursive functions to see a recursive algorithm. I will use it as a programming language.

What is the recursive function now it is easy to say? The function that calls itself, so is the recursive function. If there is no idea about the function, then this text can be seen: Function in C

With the ease of recursion, the code is easy to solve, instead of writing many code, a few lines of code can solve a problem. void f () {
f () …
}

This is a recursive function, because the function calls itself. Call it straightforward. Again the function can call a function without calling directly, the function that calls the first function. See the following generosity

void f () {
g () …
}

void g () {
f () …
}

The f function calls the g function. Again g function called the function f. This is also a recursive function.

We write a recursive function, which will take a number from the user, then from that number it will print all numbers from 1 to the number. This is a simple program. But we will write a new way in this program. Using recursion. Since the program is simple, then we can understand only a little better. And if we understand the way the program works, we will understand the recurrence. Then we can easily write complexes using all program recursions. Before I write the function before the Pseudo code [Sudo code], then see the implication in C programming.

Pseudo code is a program or an algorithm that can be used by ordinary people. These are not written using any programming language. It is written in such a way that people can understand. Below is a code named printInt, which prints all numbers from 1 to that number.

1
2
3
4
5
6
7
printInt (k) {
if (k == 0) {
return 0;
}
print (k);
printInt (k – 1);
}
The function will take an integer as the parameter. Then check the number 0? If the vacuum is there, the function of the function is finished. If 0 is not, then print the integer. And by subtracting that integer number from 1, it will call the printInt again. It means that you will call yourself.

Now if we pass 2 in the printInt function, then three copies of the function will be created. One is the value of k for 2, one is the value of k for 1. And one is the value of k for 0. When the value of k is 2
printInt (int k) {
if (k == 0) {
return 0;
}
print (k);
printInt (k – 1);
} Will print 2 when the value of k is 1
printInt (int k) {
if (k == 0) {
return 0;
}
print (k);
printInt (k – 1);
}
Print 1 when the value of k is 0
printInt (int k) {
if (k == 0) {
return 0;
}
print (k);
printInt (k – 1);
}
Because the value of k is 0, nothing will print.

So now if we pass 5 in the printInt function, then five copies of the function will be created. That means, the number of functions that will pass, the number of times the function will be copied. And this is how the recurrence works.

Now see the implications of C programming
#include

void printInt (int k) {
if (k == 0) {
return 0;
}
printf (“% d \ n”, k);
printInt (k – 1);

}

int main () {
int i;
printf (“Enter a number:”);
scanf (“% d”, & i);
printInt (i);

return 0;
}
view rawRecursion hosted with by GitHub
At first we wrote our printInt function in the program. Then we declare an integer inside the main function. It took input from user. Then we passed the intridge in the printInt function. And printInt is recursive function. Who calls us himself and makes us work.

Try using another simple program recursion. Such a number will take input from the user, then print the sum of all the numbers from 1 to the number. When the input is 4, the normal program will be calculated as the following:
sum (4) = 1 + 2 + 3 + 4
When the input will be as 5 then
sum (5) = 1 + 2 + 3 + 4 + 5
When the input will be as 6 then
sum (6) = 1 + 2 + 3 + 4 + 5 + 6

Which means:

sum (6) = sum (5) + 6 [sum (5) = (1 + 2 + 3 + 4 + 5)]
sum (5) = sum (4) + 5 [sum (4) = (1 + 2 + 3 + 4)]
sum (4) = sum (3) + 4 [sum (3) = (1 + 2 + 3)]
sum (3) = sum (2) + 3 [sum (2) = (1 + 2)]
sum (2) = sum (1) + 2 [sum (1) = (1)]
sum (1) = sum (0) + 1 [sum (0) = (0)]
sum (0) = 0
The top steps are for 6 numbers. Now we can write an easy integer for sum of n numbers, sum (n) = sum (n-1) + n

When 0 is, you will print 0, and when n will, then print sum (n) = sum (n-1) + n

And it’s written in Sudo code

sum (int n) {
if (n == 0) return 0;
else return n + sum (n-1);
}
Applying in the C program:
#include

int sumFunc (int n) {
if (n == 0)
return 0;
else
return n + sumFunc (n-1);
}

int main () {
int n, sum;
printf (“Enter the value of n:”);
scanf (“% d”, & n);
sum = sumFunc (n);
printf (“Sum of n numbers:% d”, sum);

return 0;
}
view rawrecursion_2 hosted with by GitHub
Another liberalization of recursion, I will now see how to remove Factorial using recurrence.

Factorial of a number is the product of all numbers from 1 to the number. Factorial of a number is shown as: n! Here it is assumed that Factorial is 0.

Factorial of 1
Factory of 2 * 2 = 2
Factory of 3 is 1 * 2 * 3 = 6.
Factory of 4 is 1 * 2 * 3 * 4 = 24.
Factories of 5

We can write as: factorial (n) = (n * factorial (n-1)); And since factorial (0) = 1, we can write a recursive function Sudo code for factorial:

factorial (n) {
if (n == 0)
return 1;
else
return (n * factorial (n-1));
}
factorial (0) = 1

So if we find out the factorial (3) value, the function will work like this:

factorial (3) =
3 * factorial (2)
3 * 2 * factorial (1)
3 * 2 * 1 * factorial (0)
3 * 2 * 1 * 1
= 6
Implementation in C Programming
#include

int factorial (int n) {
if (n == 0)
return 1;
else
return (n * factorial (n-1));
}

int main () {
int n, result;
printf (“Enter the value of n:”);
scanf (“% d”, & n);
result = factorial (n);
printf (“Factorial is:% d”, result);

return 0;
}
view rawFactorial hosted with gitHub
Now try to solve a problem using the recursion. For example, take a number input from the user, hold n And what the program needs to do, find out the number n fibonacci number.

Thats All . Thanks!

Updated: May 23, 2018 — 5:00 am