Page 370 - computer science (868) class 11
P. 370

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





                368368  Touchpad Computer Science-XI
   365   366   367   368   369   370   371   372   373   374   375