What is Recursion with Example?

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

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));
}
}

Comments