Page 237 - Computer science 868 Class 12
P. 237

s=s*10+r;
                        a/=10;
                    }
                    return s;
                    }

                    void main()
                    {
                        int rev=palindrome(121);
                        if(rev==121)
                            System.out.println(121+ " is a palindrome number ");
                        else
                            System.out.println(121+ " is not a palindrome number");
                    }
                    }
                 4.  Methods without return statement:

                    void sum()
                    {
                        s = a + b;
                        System.out.println("The result is:" +s);
                    }

                        Note: If a function is not returning any value, then the data type “void” is used.




                     8.7 PROGRAMS RELATED TO SIMPLE METHODS
                   1.  An Evil number is a positive whole number that has an even number of 1s in its binary equivalent. For example,
                      binary equivalent of 9 is 1001, which contains an even number of 1s. A few evil numbers are 3, 5, 6, 9, etc.
                       Design a method to accept a positive whole number and find the binary equivalent of the number and count the
                      number of 1's in it and display whether it is an Evil number or not with an appropriate message. Output the result
                      in the format given below.
                       Example 1:
                       Input: 15
                       Binary Equivalent: 1111
                       No. of 1's: 4

                      Output: Evil Number
                       Example 2:
                       Input: 26
                       Binary Equivalent: 11010
                       No. of 1's: 3
                       Output: Not an Evil Number

                 Ans.  import java.util.Scanner;
                      class EvilNumber
                      {
                          public static int check(int n)
                          {
                              if (n < 0)



                                                                                                                       235
                                                                                                            Methods    235
   232   233   234   235   236   237   238   239   240   241   242