c recursion program

      Comments Off on c recursion program

c recursion program

c recursion program

c recursion program is collection of programs which demonstrates how to use recursion in c language. There are many benefits of using recursion in c language as it will occupy very less memory and can execute your program faster compared to other logic. Here is few sample programs to help you understand how recursion works and which type of program you can convert to do your task using recursion.

c recursion program example 1

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in
		// Recursion

#include <stdio.h>
#include <conio.h>
#include <string.h>

int i=0;

void main()
{

//    static int i=0;

	printf("\nI value is : %5d",i++);

	if(i<5)
		main();

	printf("\nRam %5d \t",i);

}


 

c recursion program example 2

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in
		// Recursion

#include <stdio.h>
#include <conio.h>
#include <string.h>


void main()
{
	clrscr();
	int n;
	long int ans;

	long int factorial(int n);

	printf("Enter the value of n : ");
	scanf("%d",&n);

	ans=factorial(n);

	printf("\n Factorial is : %ld",ans);
	getch();
}
long int factorial(int n)
{
	long int fact;

	if(n==1)
		return(1);
	else
	{
		fact = n * factorial(n-1);
		printf("%5ld",fact);
	}
	return(fact);
}

 

c recursion program example 3

//Written by Adarsh Patel
//Downloaded from http://adarshspatel.in
		// Recursion

#include <stdio.h>
#include <conio.h>
#include <string.h>


void main()
{
	clrscr();
	int n;
	long int ans;

	long int sum(int n);

	printf("Enter the value of n : ");
	scanf("%d",&n);

	ans=sum(n);

	printf("\n Factorial is : %ld",ans);
	getch();

}
long int sum(int m)
{
	long int total;

	if(m==1)
		return(1);
	else
	{
		total = m + sum(m-1);
		printf("%5ld",total);
	}
	return(total);
}

 

c recursion program example : Download all programs

[sociallocker]Download[/sociallocker]