Recursion Method

Author: Ravi Poswal

Recursion is a technique that a method calls by itself. This technique breaks complicated problems into small modules that can be easily solved.

Code Example

public class Recdemo
{  
static int count=9;  
static void x()
   {  
     count--;  
     if(count>=0)
   {  
       System.out.println("hi "+count);  
 x();  
   } 
}  
public static void main(String[] args)
{  
x();  
  } 
}

FAQs

import java.util.*;
public class Fact {
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
if (number < 0) { //Negative value is not accept
System.out.println("Factorial is not defined for negative numbers.");
} else {
long answer = factorial(number);
System.out.println("Factorial of " + number + " is: " + answer);
}
sc.close();
}
}