What is Recursion?

Calling a function within itself again and again finite/infinite times is called recursion. It is complex but makes the code compact. Recursion reduces unnecessary calling of function.

Example: Write a program to find the factorial of a number using recursion?

public class Recursion{
static int factorial(int num){
If (num==1)
return 1;
else
return (n*factorial(n-1));
}
public static void main(String[] args){
System.out.println("Factorial of 5 is:"+factorial(5));
}

 

What is Recursion with Example?

Post a Comment

0 Comments