Page 406 - Computer science 868 Class 12
P. 406

Program 3     A class called Series has been defined to generate the following series:
                              1, 12, 123, 1234, 12345
                              The description of the class is given below:
                              Data Member
                              int t                      :   To store each term
                              Series()                   :   Constructor to initialise t to 0
                              void print(int n)          :   Using recursive technique to print the terms of the series

                1       import java.util.*;
                2       public class Series

                3       {
                4           int t;

                5           public Series()
                6           {

                7               t=0;
                8           }

                9           void print(int n)
                10          {

                11            if(n>5)
                12                System.out.println();

                13            else
                14            {

                15                t=t*10+n;
                16                System.out.print(t+" ");

                17                print(n+1);
                18            }
                19          }

                20          public static void main()

                21          {
                22              Series sr = new Series();
                23              sr.print(1);

                24          }
                25      }

              The output of the preceding program is as follows:
              1 12 123 1234 12345







                404404  Touchpad Computer Science-XII
   401   402   403   404   405   406   407   408   409   410   411