Page 396 - Cs_withBlue_J_C11_Flipbook
P. 396

else
                          function1(parameter)
                  }
              Let us demonstrate indirect recursion by printing ‘n’ natural numbers using recursive methods void odd(int n) and void
              even(int a).

                  class Natural
                  {
                      void odd(int n)
                      {
                          if(n>10)    // base case
                          {
                              System.out.println();
                          }
                          else
                          {
                              System.out.println(n);  // printing odd numbers
                              even(n+1);     // calling method even()
                          }
                      }
                      void even(int a)
                      {
                          if(a>10)   // base case
                          {
                              System.out.println();
                          }
                          else
                          {
                              System.out.println(a);   // printing even numbers
                              odd(a+1);   // calling method odd()
                          }
                      }
                      public static void main()   // main
                      {
                          Natural ob = new Natural();
                          ob.odd(1);
                      }
                  }
              The output of the preceding program is as follows:
              1
              2
              3
              4
              5
              6
              7
              8
              9
              10





                394394  Touchpad Computer Science-XI
   391   392   393   394   395   396   397   398   399   400   401