Lecture2 Part2
Lecture2 Part2
Lecture2 Part2
Recursive Functions
Recursive
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.
1
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.
Example1:
The following example calculates the factorial of a given number using a loop:
#include "stdafx.h"
#include "conio.h"
int factorial(int i)
{
int fac = 1;
if (i <= 1) {
return 1;
}
for (int cnt = i; cnt >= 1; cnt--)
fac *= cnt;
return fac;
}
void main() {
int i = 3;
printf("Factorial of %d is %d\n", i, factorial(i));
_getch();
}
The following example calculates the factorial of a given number using a recursive function:
#include "stdafx.h"
#include "conio.h"
int factorial(int);
void main()
{
int fact, n;
scanf_s("%d", &n);
fact = factorial(n);
printf("Factorial of %d is %d", n, fact);
_getch();
}
int factorial(int n)
{
int temp;
if (n == 0)
2
return 1;
else
return temp;
}
3
Example2:
The following example finds Sum of Natural Numbers Using loop
1.way
#include "stdafx.h"
#include "conio.h"
void main()
{
int number, result;
result = sum(number);
printf("sum=%d", result);
_getch();
}
int sum(int n)
{
int x = 0;
for (int i = n; i >=1; i--)
x += i;
return x;
}
2.way
#include "stdafx.h"
#include "conio.h"
void factorial(int n)
{
int result=1;
for (int i = 1; i <= n; i++)
result = result*i;
//return result;
printf("factorial of %d is %d\n", n, result);
}
void main()
{
int n;
printf("enter any positive number:");
scanf_s("%d",&n);
factorial(n); //call by value
_getch();
}
4
The following example finds Sum of Natural Numbers Using a recursive function
#include "stdafx.h"
#include "conio.h"
void main()
{
int number, result;
result = sum(number);
printf("sum=%d", result);
_getch();
}
int sum(int n)
{
if (n != 0)
return n + sum(n - 1); // sum() function calls itself
else
return n;
}
Initially, the sum() is called from the main() function with number passed as an
argument.
Suppose, the value of n is 3 initially. During next function call, 2 is passed to
the sum()function. In next function call, 1 is passed to the function. This process
continues until nis equal to 0.
When n is equal to 0, there is no recursive call and the sum of integers is returned
to the main() function.
5
6
Example3:
Given an integer number and we have to count the digits using recursion using C program.
In this program, we are reading an integer number and counting the total digits,
here countDigits() is a recursion function which is taking number as an argument and
returning the count after recursion process.
#include "stdafx.h"
#include "conio.h"
while (num>0)
{
num = num / 10;
count++;
}
return count;
}
void main()
{
int number;
int count ;
count = countDigits(number);
_getch();
}
#include "stdafx.h"
#include "conio.h"
/*Static variables are initialized only once.
The compiler persists with the variable till
the end of the program. Static variables can be
7
defined inside or outside the function.
They are local to the block.
The default value of static variables is zero.*/
if (num>0)
{
count++;
countDigits(num / 10);
}
else
{
return count;
}
}
void main()
{
int number;
int count = 0;
count = countDigits(number);
_getch();
}