Page 394 - Cs_withBlue_J_C11_Flipbook
P. 394

24              int h = calHCF(Math.max(a, b),Math.min(a, b));
                25              System.out.println("HCF = "+h);

                26          }
                27          public static void main()

                28          {
                29              // creating object and calling method

                30              HCF ob = new HCF();
                31              ob.accept();
                32              ob.print();

                33          } // end of class

                34      }  // end of main


              The output of the preceding program is as follows:
              Enter two numbers
              32
              24
              HCF = 8
              The dry run of the program is shown below with x = 32 and y = 24:

                                                            return 8
                         x = 8 y = 0    Is 0 = 0 true  calHCF(8, 0)        Popping last method call
                                        base case
                         x = 24 y = 8   Is 8 = 0 false  calHCF(24, 8)      Popping next method call  value
                         x = 32  y = 24  Is 24 = 0 false  calHCF(32, 24)   Popping first method call  returned
                                                                           Stack empty              8
                         Value in x &  y  Is y = 0     Method call         Method Popped


                  13.2 TYPES OF RECURSION
              There are two types of recursions which are as follows:
              •  Direct recursion

              •  Indirect recursion

              13.2.1 Direct Recursion
              Direct recursion is the normal recursive technique in which a function calls itself from its own body. This is the most
              common form of the recursive technique. It can be demonstrated as follows:
                  <return type> function1(parameter)
                  {
                      if <base case>
                          <body if true>
                      else
                          function1(parameter)
                  }


                392392  Touchpad Computer Science-XI
   389   390   391   392   393   394   395   396   397   398   399