Saturday 11 October 2014

How Recursion Work In Java

Structure of the Problem Requirements 

In this problem we will learn how the recursion done in Java. Recursion mean calling a function by himself. In this simple Program we use recursion in recursive function. In main class just initialize a variable and then pass through the Parameter of function. In function we can decrements the value of that variable by using the recursive call function. And when the value of first initialization is equal to the condition then Program terminates.

SOURCE CODE  



public class Java_Recursive {
    public static void main(Stringargs[])
    {
        int rec = 10;
        rec = recursive(rec);
         System.out.println(""+rec);
       
    }
    public static int recursive(intrec)
    {
        if(rec==5)
        {
             System.out.println(""+rec);
             return 0;
        }
        else
        return recursive(rec-1);               //here is Recursion is use
    }
}

OUTPUT OF THE PROGRAM

how recursion work in java
how recursion work in java


Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. So great post, Well explained and very helpful! Thanks.

    ReplyDelete