Sei sulla pagina 1di 4

EXAMPLES ON RECURSION IN C PROGRAMMING

Every recursive function must be provided with a way to end the


recursion.
Any program can be written using recursive function, if and only if
there exists base criteria to end the process.

1.Write a C program to find sum of first n natural


numbers using recursion.
Note: Positive integers are known as natural number i.e. 1, 2, 3....n
#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n)
{
if(n==0) ............// base criteria or base value//
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Output

Enter a positive integer:

sum = 15

In, this simple C program, sum() function is invoked from the same function.
If n is not equal to 0 then, the function calls itself passing argument 1 less
than the previous argument it was called with.
Suppose, n is 5 initially. Then, during next function calls, 4 is passed to function
and the value of argument decreases by 1 in each recursive call. When, n
becomes equal to 0, the value of n is returned which is the sum numbers from
5 to 1.
sum(5)
called function w.r.t function main()
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
Calling parts i.e. function sum() calling itself
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
Returning values
=5+4+6
=5+10
=15
Final result after the recursive calls are over (to
be retuned back
to the function main i.e. the calling function.

In this example when, n is equal to 0, there is no recursive call and recursion


ends.

2.multiply two numbers by recursion:

#include<stdio.h>
int multiply(int, int); //function prototype//
int main()
{
int a, b, product;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
product = multiply(a,b);
printf("Multiplication of two integers is %d",product);
return 0;
}
int multiply(int a, int b)
{
static int product=0,i=0;
//initially variable product is zero and further it
changes w.r.t. function calls//
if(i < a)
//Base Criteria i.e. when (i<a) is false
recursion ends//
{
product = product + b;
i++;
//processing i.e. add b for a times till i >
a
and function calls//
multiply(a,b);
}
return product;
//returns the product to the function main()//
}

3.Find power of a number using recursion using c


program

#include<stdio.h>
int main()
{
int pow, num;
long int result;
long int power(int,int);
// function prototype//
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
result=power(num,pow);
//function call in main()//
printf("\n%d to the power %d is: %ld",num,pow,res);
return 0;
}
long int power(int num,int pow)
{
int i=1;
long int sum=1;
if(i<=pow)
//Base Criteria i.e. when (i<=pow) is false recursion
ends//
{ sum=sum*num;
//processing and function calls//
power(num,pow-1);
}

else
return sum;
main()//
}

//returns the product to the function

4.Sum of digits in c using recursion

#include<stdio.h>
int main()
{
int findsum(int n);
int num,x;
printf("\nEnter a number: ");
scanf("%d",&num);
x=findsum(num);
//function call in main() //
printf("Sum of the digits of %d is: %d",num,x);
return 0;
}
int findsum(int n)
{
int r,s;
if(n>0)
//Base Criteria i.e. when (n>0) is false recursion ends//
{
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
5.

LCM of two numbers by recursion:

#include<stdio.h>
int lcm(int,int);
int main(){
int a,b,l;
printf("Enter any two positive integers ");
scanf("%d%d",&a,&b);
if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);
printf("LCM of two integers is %d",l);
return 0;
}
int lcm(int a,int b){
static int temp = 1;
if(temp % b == 0 && temp % a == 0)

//base criteria//

return temp;
temp++;
lcm(a,b); // recursive calls//
return temp;

//end of recursion//

}
Sample output:
Enter any two positive integers 5, 2
LCM of two integers is 10

Try 4 these
1. Prime number program in c using recursion
2. C program for fibonacci series using recursion
3. Reverse a string using recursion
4. Write a program for palindrome using recursion

Potrebbero piacerti anche