Happy Number Program in Java and Python
ISC 2012 Question 10 solution to check whether a number is a happy number using recursive sum of squares of digits.
Question:
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
Hence, 28 is a happy number.
Example 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:
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.
Algorithm:
Step 1: Start.
Step 2: Define a class named Happy.
Step 3: Declare an integer instance variable n.
Step 4: In the constructor, initialize n to 0.
Step 5: In getnum(nn), assign nn to n.
Step 6: In sum_sq_digits(x), if x == 0, return 0.
Step 7: Otherwise, extract the last digit as d = x % 10.
Step 8: Return d * d + sum_sq_digits(x / 10).
Step 9: In ishappy(), calculate a = sum_sq_digits(n).
Step 10: While a > 9, replace a with sum_sq_digits(a).
Step 11: If a == 1, display that n is a Happy Number.
Step 12: Otherwise, display that n is not a Happy Number.
Step 13: In main(), accept a number, create an object, call getnum() and then call ishappy().
Step 14: Stop.
Explanation:
A happy number is checked by repeatedly replacing the number with the sum of the squares of its digits. If this repeated process eventually gives 1, the number is called happy. For example, 28 becomes 68, 68 becomes 100 and 100 becomes 1. Therefore 28 is a happy number. The program follows the same idea by calculating digit-square sums again and again.
The class Happy stores the number in the instance variable n. The constructor initializes it to 0, and the method getnum(int nn) assigns the user-entered value to n. This keeps input assignment separate from the checking logic, which is the style expected in the ISC class-design question.
The method sum_sq_digits(int x) is recursive. If x becomes 0, there are no more digits left, so it returns 0. Otherwise, the last digit is extracted using x % 10. The square of this digit is added to the result of calling the same method for x / 10, which removes the last digit. In this way, each recursive call handles one digit, and the final return value is the sum of the squares of all digits.
The ishappy() method first finds the sum of square digits of the original number. Then it repeats the same operation while the result has more than one digit. The original solution stops when the result becomes a single digit. If that digit is 1, the number is printed as a Happy Number. Otherwise, it is printed as not a Happy Number. This directly follows the method demonstrated in the question examples.
Java Program:
/**
* The class Happy checks whether a given number is a happy number or not.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2012 Question 10
*/
import java.util.Scanner;
class Happy
{
int n;
Happy()
{
n = 0;
}
void getnum(int nn)
{
n = nn;
}
int sum_sq_digits(int x)
{
if(x == 0)
{
return 0;
}
else
{
int d = x % 10;
return d * d + sum_sq_digits(x / 10);
}
}
void ishappy()
{
int a = sum_sq_digits(n);
// Repeat until the result becomes a single digit.
while(a > 9)
{
a = sum_sq_digits(a);
}
if(a == 1)
System.out.print(n + " is a Happy Number");
else
System.out.print(n + " is Not a Happy Number");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Happy ob = new Happy();
System.out.print("Enter any number: ");
int b = sc.nextInt();
ob.getnum(b);
ob.ishappy();
}
}Equivalent Python Program:
# Program to check whether a number is a happy number.
class Happy:
def __init__(self):
self.n = 0
def getnum(self, nn):
self.n = nn
def sum_sq_digits(self, x):
# Recursive sum of squares of digits.
if x == 0:
return 0
else:
d = x % 10
return d * d + self.sum_sq_digits(x // 10)
def ishappy(self):
a = self.sum_sq_digits(self.n)
# Repeat until the result becomes a single digit.
while a > 9:
a = self.sum_sq_digits(a)
if a == 1:
print(self.n, "is a Happy Number")
else:
print(self.n, "is Not a Happy Number")
ob = Happy()
num = int(input("Enter any number: "))
ob.getnum(num)
ob.ishappy()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.