Page 275 - Computer science 868 Class 12
P. 275

CalSeries()
                           {
                               x=0;  n=0;  sum=0;
                           }


                           void input()
                           {
                               Scanner sc= new Scanner(System.in);
                               System.out.println("Enter x and n ");
                               x=sc.nextInt();
                               n=sc.nextInt();

                           }


                           int power(int p, int q)
                           {
                               return (q==0)? 1: p*power(p,q-1);
                           }


                           void cal()
                           {
                               for(int i=0;i<=n;i+=2)
                                   sum+= power(x, i);
                           }


                           void display()
                           {
                               System.out.println("sum = "+ sum);
                           }


                           public static void main()
                           {   Scanner sc= new Scanner(System.in);
                               CalSeries obj = new CalSeries();
                               obj.input();
                               obj.cal();
                               obj.display();
                           }
                       }
                        //Output :
                        Enter x and n
                        4
                        2
                        sum = 17
                       (ii)  Design a class Revno which reverses an integer number.                              [ISC 2022]
                          Example: 94765 becomes 56749 on reversing the digits of the number.
                          Some of the members of the class are given below:
                          Class name                                :   Revno
                          Data Members/Instance variable
                          num                                       :  to store the integer number
                          Member Functions/Methods
                          Revno()                                   :  default constructor
                          void inputnum()                           :  to accept the number
                          int reverse(int nn)                       :  returns the reverse of a number by using recursive technique




                                                                                                                       273
                                                                                                            Methods    273
   270   271   272   273   274   275   276   277   278   279   280