Page 415 - Cs_withBlue_J_C11_Flipbook
P. 415

20 is not a Prime Number

                   Program 3     Design a class Palindrome to check if a given number is a palindrome number or not. [A
                                 number is said to be palindrome if it reads same from both ends.] Examples are 121, 55,
                                 3223, 45754, etc. Some of the members of the class are given below:
                                 Class name            :   Palindrome
                                 Data members/Instance variables

                                 num                   :   To store the number
                                 rev                   :   To store its reverse
                                 Methods/Member functions
                                 Palindrome (int nn)   :     Parameterised  constructor to initialise  the data members
                                                           num=nn and rev to 0
                                 int revnum(int i)     :   Returns the reverse of the number num, using a recursive technique
                                 void check()          :     Checks whether the given number is palindrome or not,  by
                                                           invoking the function revnum(int) and displays the result with
                                                           an appropriate message
                                   Specify  the  class  Palindrome giving  details  of the  constructor,  int  revnum(int)  and  void
                                 check(). Define a main() function to create an object and call the functions accordingly to
                                 enable the task.

                   1         class Palindrome
                   2       {

                   3           int num,rev; // data members
                   4           // parameterised constructor

                   5           Palindrome(int nn)
                   6           {

                   7               num=nn;
                   8               rev=0;
                   9           }

                   10          int revnum(int i)

                   11          {
                   12              if(i==0) // base case
                   13                  return rev;

                   14              else


                                                                                                                       413
                                                                                                           Recursion   413
   410   411   412   413   414   415   416   417   418   419   420