Page 438 - Computer science 868 Class 12
P. 438
17. A Happy number is a number in which the eventual sum of square of digits of the number is equal to 1. For example,
68 Sum of square of digits = 6 + 8 = 100
2
2
2
2
100 Sum of square of digits = 1 + 0 + 0 = 1
2
Then 68 is a happy number. Design a class Happy to check if a given number is a happy number or not. Some of the members of
the class are given below.
Class name : Happy
Data Members
n : stores the number
Member Methods
Happy () : constructor to assign 0 to n
void getnum(int nn) : to assign the parameter value to the number, n = nn
int sumSq digits(int) : return the sum of square of the digits of number
void isHappy () : checks if the given number is a Happy number by calling the function
SumSqdigits(int) and displays appropriate message
Specify the class Happy by giving details of the constructor, void getnum(int), int sumSqdigits(int) and void isHappy(). Write the
main function to create object and call other functions.
18. Design a class Prime to check if a given number is a prime number or not. [A number is said to be prime if it is only divisible by 1
and itself].
Example: 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Some of the members of the class are given below:
Class name : Prime
Data Members/Instance variables:
num : to store the number
Methods/Member functions
Prime (int nn) : parameterised constructor to initialise the data member num=nn
int countfactors(int i) : returns the count of the factors of the number(num), using a recursive technique
void check() : checks whether the given number is prime or not by invoking the function
countfactors(int) and displays the result with an appropriate message
Specify the class Prime giving details of the constructor(), int countfactors(int) and void check(). Define a main() function to create
an object and call the functions accordingly to enable the task.
19. Design a class Pattern which will accept number of lines as n and print the pattern. If n = 5 the pattern is
12345
2345
345
45
5
The details of the class are given below.
Data Members
int n : to store number of lines
Member Methods
void read(int nn) : initialise nn to n
void print(int i, int j) : using the concept of recursion design the method print(int, int) which will
generate the above pattern
static void main() : create object and call the other methods
20. Design a class Palindrome to check if a given number is a palindrome number or not. [A number is said to be palindrome if it reads
same from both ends]. Example: 121, 55, 3223, 45754, etc. Some of the members of the class are given below:
Class name : Palindrome
Data Members/Instance variables
num : to store the number
rev : to store reverse
436436 Touchpad Computer Science-XII

