3542 views.
class HelloRecursion {
//printUptoOne method prints n to 1 values.
public static void printUptoOne(int n) {
//base case n == 0 exit
if (n == 0) {
return;
}
else {
//recursive case: n != 0 then print n and call n-1 to print.
System.out.println(n);
printUptoOne(n-1);
}
}
public static void main(String[] args) {
/*
* Simple recursion
* To print 5,4,3,2,1 series by using recursion
*
* Recursion: Recursion calling itself again and again.
* Has two cases: Base case and Recursive case
*
* Base case: To ensure recursion terminates.
* Recursive case: To call itself again for sub process.
*
*/
//Calling recursive method with n = 5;
printUptoOne(5);
}
}