Page 505 - Cs_withBlue_J_C11_Flipbook
P. 505
Variable Description
NAME TYPE DESCRIPTION
ar[][] int[][] Array elements
i int Loop Variable
j int Loop Variable
m int Number of rows
n int Number of columns
s int Sum of rows/columns
Program 8 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.*;
2 class magic
3 {
4 int num[];int n;
5 magic(int s)
6 {
7 n=s;
8 }
9
10 void input()
11 {
12 Scanner sc =new Scanner(System.in);
13 num=new int[n];
14 System.out.print("Enter elements one by one in the array");
15 for(int i=0;i<n;i++)
16 {
503
Internal Assessment 503

