Page 206 - Computer science 868 Class 12
P. 206
121 is a palindromic number
Enter a number
16
Enter p/P for Palindrome / n/N for Niven
n
16 is not a Niven Number
Program 8 A happy number is a number in which the eventual sum of the square of the digits of the
number is equal to 1.
Example 1:
2
28 = (2) + (8) = 4 + 64 = 68
2
68 = (6) + (8) = 36 + 64 = 100
2
2
100 = (1) + (0) + (0) = 1 + 0 + 0 = 1
2
2
2
Hence, 28 is a happy number.
Example 2:
2
12 = (1) + (2) = 1 + 4 = 5
2
Hence, 12 is not a happy number.
Design a class Happy to check if a given number is a happy number. Some of the members of
the class are given below:
Class name : Happy
Data Members/Instance variables
n : Stores the number of Member functions
Happy() : Constructor to assign 0 to n
void getnum(int nn) : Assigns the parameter value to the number n = nn
int sum_sq_digits(int x) : Returns the sum of the square of the digits of the number
x using the recursive technique
void ishappy() : Checks if the given number is a happy number by
calling the function sum_sq_digits(int) and displays an
appropriate message
Specify the class Happy giving details of the constructor(), void getnum(int), int sum_sq_
digits(int) and void ishappy(). Also, define a main() function to create an object and call the
methods to check for a happy number.
1 import java.util.*;
2 class Happy
3 {
4 int n, s, d;
5 public Happy()
6 {
7 n = 0;
8 }
9
10 void getnum(int nn)
11 {
204204 Touchpad Computer Science-XII

