Recursion In C
A function that calls itself is known as a recursive
function. And, this technique is known as recursion.
What is Recursion?
The
process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.
Syntax:
void
recurse()
{
…
…. …
recurse();
…
… …
}
int main()
{
… … …
recurse();
… … …
}
Program:
#include<stdio.h>
int sum(int n);
int main()
{
int number, result;
printf(“enter a
positive integer:”);
scanf(“%d”,
&number);
result=sum(number);
printf(“sum
=%d”,result);
return 0;
}
int sum(int n)
{
if(n!=0)
return n+sum(n-1);
else
return n;
}
Output:
Enter a positive
integer : 3
sum=6
No comments:
Post a Comment