Recursion in C | Recursive Function | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Wednesday, July 8, 2020

Recursion in C | Recursive Function | My CS Tutorial

Recursion in C programming | Examples of recursion | Recursive functions | C- recursion | My CS Tutorial

Recursion in C | My CS Tutorial

Recursion in c | C- Recursive function


The C programming language supports recursion, i.e., a function to call itself.
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.

Recursion function works in two phase


  1. Winding phase.
  2. Unwinding phase.
Winding phase: In Winding phase, the recursive function keeps calling itself. This phase ends when the base condition is reached.
Unwinding phase: When the base condition is reached Unwinding phase starts and control return backs on original call.

Number Factorial


A simple c program to find the factorial of a given number using recursion.

The following example calculates the factorial of a given number using a
recursive function:-

#include <stdio.h>
int factorial(unsigned int i)
{
 if(i <= 1)
 {
 return 1;
 }
 return i * factorial(i - 1);
int main()
{
 int i = 15;
 printf("Factorial of %d is %d\n", i, factorial(i));
 return 0;
}

When the above code is compiled and executed, it produces the following result:-

Factorial of 5 is 120

Fibonacci Series

A simple c program to print the Fibonacci series for a number using recursion.

The following example generates the Fibonacci series for a number using a recursive function:-

#include <stdio.h>
int fibonaci(int i)
{
 if(i == 0)
 {
 return 0;
 }
 if(i == 1)
 {
 return 1;
 }
 return fibonaci(i-1) + fibonaci(i-2);
}
int main()
{
 int i;
 for (i = 0; i < 8; i++)
 {
 printf("%d\t%n", fibonaci(i));
}
 return 0;
}

When the above code is compiled and executed, it produces the following result:-

0 1 1 2 3 5 8 13


Recursion in C programming | Examples of recursion | Recursive functions | C- recursion | My CS Tutorial

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment