Java Recursion


In Java language, Recursion is a programming technique that the method calls itself is called Recursion, which means that if a program allows the programmer to call a method within in the same function, it’s called Recursion call of the method.

Java recursion

Syntax:-

returntype recursion() { recursion(); /* method calls itself */ } 

Example:-

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

notepad

The programmer has to define exit condition from the Method, if not it will go into an infinite loop.




Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.