[Java Basics] Recursive invocation

Source: Internet
Author: User

Recursive invocation : by invoking or indirectly invoking the program itself

recursive invocation The most important point is that there must be a head, if there is no header , has been called down , It is a dead loop .

code example:

/*** Recursive calling code example <br> * Description: A method that calls itself directly or indirectly .<br> *@authorrushing out of the earth **/ Public classRecursion {/*** Sample program: Factorial <br> * Factorial of a number, that is, from 11 straight to that number <br> * Example: 2! = 1*2 5! = 1*2*3*4*5<br> * Decomposition: 5! = 5*4! = 5*4*3! = 5*4*3*2! = 5*4*3*2*1<br> *@returnreturns the product of the factorial of I and i-1, i.e. i* (i-1)! * */    Private Static intGetfactorial (inti) {        if(i = = 1) {            return1; } Else {            returnI*getfactorial (i-1); }    }        /*** Sample Program: Add <br> * Randomly specify a number, the sum from 1 to this number and <br> * Example: 5 of the cumulative = 5+4+3+2+1<br> * Decomposition: 5 of the cumulative = 5+4 tired additive = 5+4+3 = 5+4+3+2+1<br> *@authorrushing out of the earth *@paramThe number to which I accumulate *@seerecursion#getsumbycirculation (int) cumulative for loop method **/    Private Static intGetsumbyrecursion (inti) {        if(i = = 1) {            return1; } Else {            returni + getsumbyrecursion (i-1); }    }        /*** Cumulative for Loop method <br> *@authorrushing out of the earth *@paramThe number to which I accumulate *@seerecursion#getsumbyrecursion (int) Cumulative recursive invocation method **/    Private Static intGetsumbycirculation (inti) {        intresult = 0;  for(intj = 1; J <= I; j + +) {result+=J; }        returnresult; }        /*** Main program entrance **/     Public Static voidMain (string[] args) {//ask for factorial of 5        intFACTORIAL5 = getfactorial (5); System.out.println (The factorial of 5 is: "+FACTORIAL5); //use the For loop to get the 100 accumulation        intsumByCirculation100 = getsumbycirculation (100); System.out.println (The cumulative value of 100 calculated using the For loop is: "+sumByCirculation100); //use recursive calls to get 100 of the cumulative        intsumByRecursion100 = getsumbyrecursion (100); System.out.println ("The cumulative value of 100 calculated with recursive invocation is:" +sumByRecursion100); }    }

[Java Basics] Recursive invocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.