Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
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.
Below is the source code for C Program to Print pyramid of numbers using Recursion which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Print pyramid of numbers */
#include<stdio.h>
void func1(int n);
void func2(int n);
void func3(int n);
int main( )
{
int n;
printf("Enter how many lines u want to print ? ");
scanf("%d",&n);
printf("\n------------ Pattern 1 ----------- \n\n");
func1(n);
printf("\n");
printf("\n------------ Pattern 2 ----------- \n\n");
func2(n);
printf("\n");
printf("\n------------ Pattern 3 ----------- \n\n");
func3(n);
return 0;
}
void func1(int n)
{
int i;
if(n==0)
return;
else
{
func1(n-1);
for(i=1; i<= n; i++)
printf("%d ",i);
printf("\n");
}
}
void func2(int n)
{
int i;
if(n==0)
return;
else
{
for(i=1; i<=n; i++)
printf("%d ",i);
printf("\n");
func2(n-1);
}
}
void func3(int n)
{
int i;
if(n==0)
return;
else
{
for(i=n; i>=1; i--)
printf("%d ",i);
printf("\n");
func3(n-1);
}
}
Recursion : :
Below is the source code for C Program to Print pyramid of numbers using Recursion which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
need an explanation for this answer? contact us directly to get an explanation for this answer