Page 273 - Computer science 868 Class 12
P. 273
12. Design a class ArmNum to check if a given number is an Armstrong number or not. A number is said to be an Armstrong number
if the sum of its digits raised to the power of the length of the number is equal to the number.
Example:
3
3
371 = 3 + 7 + 1 3
4
4
4
1634 = 1 + 6 + 3 + 4 4
5
54748 = 5 + 4 + 7 + 4 + 8 5
5
5
5
Thus, 371, 1634 and 54748 are all examples of Armstrong numbers.
Some of the members of the class are given as follows.
Class name : ArmNum
Data Members/Instance variables
n : to store the number
l : to store the length of the number
Member Methods/Member functions
ArmNum (int nn) : parameterised constructor to initialise the data member n = nn
int sum_pow(int i) : returns the sum of each digit raised to the power of the length of the number using
2
recursive technique, e.g., 34 will return 3 + 4 (as the length of the number is 2)
2
void isArmstrong() : checks whether the given number is an Armstrong number by invoking the function
sum_pow () and displays the result with an appropriate message.
Specify the class ArmNum giving details of the constructor(), int sum_pow(int) and void isArmstrong(). Define a main() function
to create an object and call the functions accordingly to enable the task.
13. Design a class isbn to check if a given number is an ISBN number or not.
An ISBN (International Standard Book Number) is a ten-digit code that uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to check whether ISBN is correct
or not.
Example: For an ISBN “1259060977”
Sum = 1*10 + 2*9 + 5*8 + 9*7 + 0*6 + 6*5 + 0*4 + 9*3 + 7*2 + 7*1 = 209
Now divide it with 11. If the remainder is equal to 0, then it is a valid ISBN.
Some of the members of the class are given below.
Class name : isbn
Data Members/Instance variables
n : to store the number
Member Methods/Member functions
isbn (int nn) : parameterised constructor to initialise the data member n = nn
void is_isbn() : checks whether the given number is an ISBN number and displays the result with an
appropriate message
Specify the class isbn giving details of the constructor(), void is_isbn(). Define a main() function to create an object and call the
functions accordingly to enable the task.
14. A class Adder has been defined to add any two accepted times.
Example:
Time A – 6 hours 35 minutes
Time B – 7 hours 45 minutes
Their sum is – 14 hours 20 minutes ( where 60 minutes = 1 hour)
The details of the members of the class are given as follows.
Class name : Adder
Data Members/Instance variable
a[] : integer array to hold two elements (hours and minutes)
Member Functions/Methods
Adder() : constructor to assign 0 to the array elements
void readtime() : to enter the elements of the array
void addtime( Adder X, Adder Y) : adds the time of the two parameterised objects X and Y and stores the sum in the
current calling object
void disptime() : displays the array elements with an appropriate message
Specify the class Adder giving details of the constructor(), void readtime(), void addtime(Adder, Adder) and void disptime().
Define the main() function to create objects and call the functions accordingly to enable the task.
271
Methods 271

