32-Function Recursion
32-Function Recursion
Objectives:
To learn and understand the following concepts:
• Understand recursion
• Write simple programs using recursive functions
A child couldn't sleep, so her mother told a story about a little frog,
who couldn't sleep, so the frog's mother told a story about a little
bear,
who couldn't sleep, so bear's mother told a story about a little
weasel
...who fell asleep.
...and the little bear fell asleep;
...and the little frog fell asleep;
...and the child fell asleep.
Recursive steps:
Identify the base case for the algorithm.
Call the same function recursively with the parameter
having slightly modified value during each call.
This makes the algorithm move towards the base case and
finally stop the recursion.
So factorial(5)
= 5* factorial(4)
= 4* factorial(3)
= 3*factorial(2)
= 2* factorial(1)
=
1*factorial(0)
05/16/2025 CSE 1001 Department of CSE 10
Factorial- recursive procedure
#include <stdio.h>
long factorial (long a) {
if (a ==0) //base case
return (1);
return (a * factorial (a-1));
}
int main () {
long number;
printf("Please type a number: “);
scanf(“%d”, number);
printf(“number ! = %d”, factorial (number));
return 0;
}
x =2 = 120
5 rFact(4)
4
Notice that the
recursion isn’t x
finished at the 4 rFact(3) = = 24
6
bottom --
It must unwind all x =
3 rFact(2) = 6
the way back to the
2
top in order to be
factorial(0) = 1
done.
factorial(n) = n * 2
x
rFact(1) = = 2
factorial(n-1) [for n>0] 1
So fibonacci(4)
= fibonacci(3) + fibonacci(2)
= (fibonacci(2) + fibonacci(1)) + (fibonacci(1) + fibonacci(0))
= ((fibonacci(1) + fibonacci(0)) + 1) + (1 + 0)
= ( 1 + 0 ) + 1) + (1 + 0)
=3
int main(void){
int n,i, a[20], fibo;
printf("enter any num to n\n“);
scanf(“%d”, n);
printf(“Fibonacci series “);
for (i=1; i<=n; i++)
{
fibo = rfibo(i);
printf(“%d”, fibo);
}
return 0;
}
05/16/2025 CSE 1001 Department of CSE 16
Static Variable:
The value of static variable persists until the end of the
program.
Static variables can be declared as
static int x;
A static variable can be either an internal or external
type depending on the place of declaration.
void fnStat( );
Output:
int main() { void fnStat( ){ x=1
int i; static int
static intx x==0;0; x=1 2
for( i= 1; i<=3; x = x + 1; x=1 3
i++) printf(“x=%d”,
fnStat( ); x);
05/16/2025 return 0; }
CSE 1001 Department of CSE 17
GCD: Recursion
Output:
x= 24 , y = 9
gcd = 3
05/16/2025 CSE 1001 Department of CSE 18
Recursion - Should I or Shouldn’t I?
• Pros • Cons
– Recursion is a – Recursive programs
natural fit for typically use a large
recursive problems amount of computer
memory and the greater
the recursion, the more
memory used
– Recursive programs can
be confusing to develop
and extremely
complicated to debug
RECURSION ITERATION
return 0;
}
05/16/2025 CSE 1001 Department of CSE 24
Extra Problem- Sum of natural numbers
#include <stdio.h> int sum(int num)
int sum(int n); {
if (num!=0)
return num + sum(num-1);
int main()
else
{ return num;
int number, result; }
printf("sum=%d", result);
}
05/16/2025 CSE 1001 Department of CSE 25
Extra Problem- To count number of digits
#include <stdio.h>
int countDigits(int num)
int countDigits(int);
{
int main() static int count=0;
{
int number; if(num>0)
int count=0; {
count++;
printf("Enter a positive integer number: "); countDigits(num/10);
}
scanf("%d",&number);
else
{
count=countDigits(number); return count;
}
printf(“Number of digits is: %d\n",count); }
Output:
return 0; Enter a positive integer number: 123
Number of digits is: 3
}
05/16/2025 CSE 1001 Department of CSE 26
Extra Problem- To find sum of all digits
#include <stdio.h>
int sumDigits(int num)
int sumDigits(int num); {
int main() static int sum=0;
{ if(num>0)
int number,sum; {
sum+=(num%10);
sumDigits(num/10);
printf("Enter a positive integer number: ");
}
scanf("%d",&number); else
{
sum=sumDigits(number); return sum;
}
printf("Sum of all digits are: %d\n",sum); }
Output:
return 0; Enter a positive integer number: 123
} Number of digits is: 3
05/16/2025 CSE 1001 Department of CSE 27
Extra Problem-Reversing a Number
#include <stdio.h>
int length(char [], int); int length(char str[], int index)
{
int main()
if (str[index] == '\0')
{
char str[20]; {
int count; return 0;
}
printf("Enter any string :: "); return (1 + length(str, index + 1));
scanf("%s", str); }
count = length(str, 0);
printf("The length of string=%d.\n",count);
return 0;
Output:
}
Enter any string :: Manipal
The length of string= 7
05/16/2025 CSE 1001 Department of CSE 29
Extra Problem-Binary Search
#include<stdio.h>
int binarySearch(int x[],int element,int start,int end);
int main(){
int x[20],n,i,index,start=0,end,element;
printf("Enter number of elements: ");
scanf("%d",&n);
end = n;
printf("Enter array elements: ");
for(i=0;i<n;i++){
scanf("%d",&x[i]);
}
printf("Enter the element to search: ");
scanf("%d",&element);
index = binarySearch(x,element,start,end-1);
if(index == -1)
printf("Element Not Found.\n");
else
printf("Element found at index : %d\n",index);
return 0;
05/16/2025 CSE 1001 Department of CSE 30
}
Extra Problem-Binary Search
int binarySearch(int x[],int element,int start,int end){
int mid,noOfElements,i;
mid = (int)(start+end)/2;
if(start > end)
return -1;
if(x[mid] == element)
return mid;
else if(x[mid] < element){
start = mid+1;
binarySearch(x,element,start,end);
}
else{
start = 0;
end = mid-1; Output:
binarySearch(x,element,start,end); Enter number of
} elements: 5
Enter array elements: 1
2345
} Enter the element to
search: 3
05/16/2025 CSE 1001 Department of CSE 31
Element found at
Extra Problem- Recursive Sorting
Base Case:
if length of the list (n) = 1
No sorting, return
Recursive Call:
1. Find the smallest element in the list and
place it in the 0th position
2. Sort the unsorted array from 1.. n-1
sortR(&list[1], n-1)
{-2,33,0,2,4} sort(&list[1],4)
{-2,0,33,2,4} sort(&list[1],3)
{-2,0,2,33,4} sort(&list[1],2)
{-2,0,2,4,33} sort(&list[1],1)