Page 226 - Computer science 868 Class 12
P. 226

7. The following is a function of some class which checks if a positive integer is a Palindrome number by returning true or false.
                    (A number is said to be palindrome if the reverse of the number is equal to the original number.) The function does not use
                    the modulus (%) operator to extract digit. There are some places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which may be
                    replaced by a statement/expression so that the function works properly.

                Ans.  boolean PalindromeNum(int N)
                    {
                    int rev=?1?;
                    int num=N;
                    while(num>0)
                    {
                    int f=num/10;
                    int s= ?2?;
                    int digit = num-?3?;
                    rev= ?4? + digit;
                    num/= ?5?;
                    }
                    if(rev==N)
                    return true;
                    else
                    return false;
                    }
                    1.  What is the statement or expression at ?1?
                    2.  What is the statement or expression at ?2?
                    3.  What is the statement or expression at ?3?
                    4.  What is the statement or expression at ?4?
                    5.  What is the statement or expression at ?5?
                Ans.  (i)  0
                    (ii)  f*10
                     (iii) s
                     (iv) rev*10
                    (v) 10
                  8. The following function magicfun() is a part of some class. What will the function magicfun() return, when the value of n=7 and
                    n=10, respectively? Show the dry run/working:                                              [ISC 2017]

                    int magicfun (int n)
                    {
                    if(n = = 0)
                    return 0;
                    else
                    return magicfun(n/2) * 10 + (n%2);
                    }
                Ans.  Dry run of the function when n = 7
                    magicfun(7)
                            magicfun(3) * 10 + 1
                                 magicfun(1) *10 + 1
                                      magic fun(0) *10 + 1  = 111
                     If n = 7, the function will return 111
                     Dry run of the function when n = 10
                    magicfun(10)
                               magicfun(5) * 10 + 0
                                   magicfun(2) *10 + 1
                                      magicfun(1) *10 + 0
                                         magicfun(0) *10 + 1  = 1010
                     If n = 10, the function will return 1010. It is the binary equivalent of n


                224224  Touchpad Computer Science-XII
   221   222   223   224   225   226   227   228   229   230   231