Problem Solving using C Language

0 of 77 lessons complete (0%)

Functions

Recursion

Recursion is a special case where function calls itself repeatedly.

main()
{
	printf(“this is the example of recursion”);
	main();
}

Another example is factorial:

Factorial of n=n(n-1)(n-2)…….1
Factorial of 4=4*3*2*1 =24

#include<stdio.h>

int factorial(int);
void main()
{
    int result,b=5;
	printf("this is the example of recursion");
	result=factorial(b);
	printf("\n Result is : %d", result);
}

int factorial(int n)
{
	int fact;
	if(n==1)
		return 1;
	else
		fact=n*factorial(n-1);
	return(fact);
}

-----------------------------------------------------
Iteration Working
fact=3*factorial(2)
fact=3*2*factorial(1)
fact=3*2*1
fact=6