Page 579 - ComputerScience_Class_11
P. 579

Program 9     Write a program to input an array of integers and find if there are any magic numbers in the
                                 array. The specifications of the class magic are given below.
                                 class                           :  Magic
                                 Data Members
                                 int num[]                       :  single dimensional array to store number
                                 int n                           :  size of array
                                 Member Methods
                                 magic(int s)                    :  constructor to store s to n
                                 void input()                    :   inputs value to num
                                 int digit_sum(int i)            :  returns the sum of the digits in i
                                 void find_magic()               :   calls the digit_sum(int) and prints all the magic numbers
                                                                   in the array
                                 eg: Magic number: 289=2+8+9=19=1+9=1
                                 So, magic number is a numbers whose ultimate sum of the digits is 1.

                   1       import java.util.Scanner;
                   2

                   3       class Magic

                   4       {

                   5           int num[];
                   6           int n;

                   7           Magic(int s)

                   8           {

                   9               n = s;
                   10              num = new int[n];

                   11          }

                   12

                   13          void input()
                   14          {

                   15              Scanner sc = new Scanner(System.in);

                   16              System.out.println("Enter " + n + " integers:");
                   17              for (int i = 0; i < n; i++)

                   18              {

                   19                  num[i] = sc.nextInt();

                   20              }

                   21          }
                   22




                                                                                                Internal Assessment  577
   574   575   576   577   578   579   580   581   582   583   584