Professional Documents
Culture Documents
DATE:06-02-2018
RECURSION
Recursion is a powerful technique of writing a complicated algorithm in
an easy way. According to this technique a problem is defined in terms of itself.
The problem is solved by dividing it into smaller problems, which are similar in
nature to the original problem. These smaller problems are solved their
solutions are applied to get the final solution of our original problem.
To implement recursion technique in programming, a function should be
capable of calling itself this facility is available in C. The function that call itself
(inside function body) again and again is known as a recursive function. In
recursion the calling function and the called function are same.
For example:
main()
{
..........
rec ( ) ;
.........
}
rec ( )
{
.........
...........
rec ( ); -------- recursive call
..............
}
Before writing a recursive function for a problem we should consider these points
1. We should be able to define the solution of the problem in terms of a similar
type of smaller problem.
At each step we get closer to' the final' solution of our original problem.
2. There should be a terminating condition to stop recursion.
Program : Factorial by using recursion
#include<stdio.h>
int main()
{
int num,res;
printf(“enter the number:”);
scanf(“%d”,&num);
res=fact_recursion(num)
printf(“%d”,res);
}
int fact_recursion(int num)
{
int res=1;
if (num==1)
return res;
else
res=num*fact_recursion(num-1);
retunn res;
}
Input:5
Output:120
#include<stdio.h>
void print_numbers(int num);
int main()
{
int num;
printf(“enter a num:”);
scanf(“%d”,&num);
printf_numbers(int num);
return 0;
}
void print_numbers(int num)
{
if(num==0)
return;
print_num(num-1); //interchange these two lines and check
printf(“%d”,num); //the output
return;
}
Output:
5,1
2
3
4
5
ENUM (Enumeration)
Enumeration (or enum) is a user defined data type in C. It is mainly used to
assign names to integral constants, the names make a program easy to read and
maintain.
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}