Web Development, SEO, Social Media, Software & Database Development
Java Recursion Example
Today im covering recursion, recursion is a method that calls itself, this may not sound like a great idea as it has implications of a never ending loop.
class recursionExample
{
public static void main(String args[])
{
// call method Rec with vaule 5
System.out.println(Rec(5));
}
//calls itself 5 times decrementing each time until it reaches 1 or less.
public static long Rec (long x)
{
if (x<=1)
return 1;
else
{
return x*Rec(x-1);
}
}
}
----------------------------OUTPUT-------------------
120
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment